Split question

Hello everyone;

Can you help me with the following problem :

I have the following list :

txtList = “1,Oranges;2,Apples;3,Lemons;4,Peers”

Everything separated by the “;” delimiter is a record and belongs to each other.

So when doing the following :

[code]Using Xojo.Core // Using the new framework

Dim txtList As Text
Dim txtRecord(-1) As Text
Dim txtSubRecord(-1) As Text
Dim strList As Text

// Assigning values to the list
txtList = “1,Oranges;2,Apples;3,Lemons;4,Peers”

// Splitting the list into records
txtRecord = txtList.Split(";") // Now txtRecord contains 4 elements (zero based)

// Get the 2nd element
MsgBox txtRecord(1) // shows “2,Apples”

// Splitting the 2nd element based on the “,” character
txtSubRecord = txtRecord(1).Split(",") // txtSubRecord is contains now 2 elements, element 0 = 2, element 1 = Apples
MsgBox "ID : " + txtSubRecord(0) + " Fruit : " + txtSubRecord(1) // Shows ID : 2 Fruit : Apples
[/code]

The split is succesfull and I get exactly what I need. However I wish to do that in one line. So on the moment I split the record with the “;” sign I want to immediatelly split the record and the subrecords.

My logic is flawed, when I overlook this code again. It is simply impossible to make my request in one pass because to get those sub-records we need to iterate through txtRecord for each record it consists.

However I decided to post it anyway, in case someone use a split example in the new framework.

Friendly greetings,

Chris

No, it is not. There is no “one-liner” for this (unless you create a function for it yourself and you move the code above into that function).

Function MySplit(t As Text) As Text(,) Dim result(-1, -1) As Text Dim records() As Text = t.Split(";") ReDim result(records.Ubound, 1) For i As Integer = 0 To records.Ubound Dim parts() As Text = records(i).Split(",") result(i, 0) = parts(0) result(i, 1) = parts(1) Next Return result End Function

[code]Dim txtList As Text = “1,Oranges;2,Apples;3,Lemons;4,Peers”

Dim records(-1, -1) As Text = MySplit(txtList)

MsgBox(records(0, 0) + " – " + records(0, 1)) // 1 – Oranges
MsgBox(records(1, 0) + " – " + records(1, 1)) // 2 – Apples
…[/code]

well if really putting everything in one line is more priorized than code readability then you could consider the use of .Left, .Right, .Len and .InStr

(just written down not checked in IDE)

txtRecord(1).left(txtRecord(1).inStr(",")+1)+" - "+txtRecord(1).right(txtRecord(1).len-txtRecord(1).inStr(","))

But I would not suggest this…

Use NthField instead of split.

MsgBox "ID "+ NthField(NthField(txtList, ";", 2), ",", 1)+ "    Fruit: "+ NthField(NthField(txtList, ";", 2), ",", 2)

Why not using JSON?

It is dealing very well with key / value pairs. And it converts from / to text as well.
So saving to disc works very well, as well as sending it over the net.

xojo.data
xojo.core.dictionary

only thing is that a dictionary key needs to be a text-based value, not an integer. A JSON array might be a solution, in that case

Hum. Key is a variant. You can have anything used for key. Integers and kitchen sinks.

http://documentation.xojo.com/index.php/Dictionary.Key

I sort of tried that. It resulted in an error with some sort of message that the Key value has to be a string value. String sounds kinda funny, since I tested it in an iOS environment.

Xojo.Core.Dictionary (new framework) uses Auto keys and value.It can be an integer.

This works perfectly in iOS :

Dim d As New Dictionary d.Value(1234) = 1 dim result as integer = d.Value(1234) system.DebugLog result.ToText

Are you sure there is not something else ? String is unknown in iOS.

You can use the “extend” aproach too.

Define a new method in a module with the following signature/code:

[code]Function splitWithSubgroup(extends o as string, d1 as string, d2 as string) As pair()
dim globalGroup() as string = o.split(d1)

dim result() as pair

for each item as string in globalGroup

result.Append new pair(item.NthField(d2,1), item.NthField(d2,2))

next

Return result
End Function[/code]

Then you can call this method from any String:

[code] dim txtlist as string = “1,Oranges;2,Apples;3,Lemons;4,Peers”

dim p() as pair = txtlist.splitWithSubgroup(";",",")

for each item as pair in p

MsgBox "Item: " + item.Left.StringValue + " Value: " + item.Right.StringValue

next[/code]

Javier Rodrguez