LessonsIntermediate: Stacks, objects and dataLesson 33

Split, combine and friends

Turn text into arrays and back with split and combine, merge arrays with union and intersect, and do sums over a whole array.

  • Intermediate
  • Lesson 33 of 83
  • About 9 minutes
  • 10 examples

Text into an array: split

split cuts text into the elements of an array. Split by one delimiter and the pieces are numbered from 1:

ExampleRun it in the playground
put "Name,Age" & return & "Ada,36" & return & "Alan,41" into tTable
split tTable by return
put the number of elements of tTable
put tTable[2]
put item 1 of tTable[3]
Output
3
Ada,36
Alan

Split by two delimiters and each piece becomes a key with its value: the first delimiter separates the pairs, the second the key from the value.

ExampleRun it in the playground
put "apple=3,pear=5,fig=1" into tStock
split tStock by comma and "="
put tStock["pear"]
put the number of elements of tStock
add 10 to tStock["fig"]
put tStock["fig"]
Output
5
3
11

That makes settings files, query strings and simple records easy to read.

An array into text: combine

combine is the reverse: it joins the elements into text. With one delimiter you get the values, in the order of their keys when the keys are numbers; with two, the keys and values.

ExampleRun it in the playground
put "red" into tColours[1]
put "green" into tColours[2]
put "blue" into tColours[3]
combine tColours with comma
put tColours
put 3 into tStock["apple"]
put 5 into tStock["pear"]
combine tStock with return and ": "
sort tStock
put tStock
Output
red,green,blue
apple: 3
pear: 5

Split as a set

as set makes every piece a key, with true as its value: a quick way to ask "is this one of them?", and to drop duplicates, as Filtering lists showed.

ExampleRun it in the playground
put "mon,tue,wed,thu,fri" into tWorkdays
split tWorkdays by comma as set
put tWorkdays["wed"]
put tWorkdays["sat"] is true
Output
true
false

Merging arrays

union adds to an array every key of another that it does not have yet; the keys it already has keep their values. intersect keeps only the keys both arrays have.

ExampleRun it in the playground
put 1 into tA["x"]
put 2 into tA["y"]
put 3 into tB["y"]
put 4 into tB["z"]
put tA into tBoth
union tBoth with tB
put the keys of tBoth into tKeys
sort tKeys
put tKeys
put tBoth["y"]
put tA into tShared
intersect tShared with tB
put the keys of tShared
Output
x
y
z
2
y

Add recursively to merge arrays inside arrays too, a key at a time, which is how you lay a user's settings over the defaults:

ExampleRun it in the playground
put 12 into tDefaults["text"]["size"]
put "black" into tDefaults["text"]["colour"]
put 18 into tSettings["text"]["size"]
union tSettings with tDefaults recursively
put tSettings["text"]["size"] && tSettings["text"]["colour"]
Output
18 black

Sums over an array

The list functions you met in Math functions take an array as well as a list, and work on its elements:

ExampleRun it in the playground
put "4,8,15,16,23,42" into tNumbers
split tNumbers by comma
put sum(tNumbers)
put max(tNumbers) && min(tNumbers)
put average(tNumbers)
Output
108
42 4
18

How far a numbered array goes

A numbered array can have gaps. the extents gives the smallest and largest number used, which can differ from the number of elements:

ExampleRun it in the playground
put "a" into tA[1]
put "b" into tA[2]
put "c" into tA[3]
delete variable tA[2]
put the extents of tA
put the number of elements of tA
Output
1,3
2

Saving an array

An array cannot be written to a file or kept in a field as it is. arrayEncode turns it into binary data that can, and arrayDecode brings it back, nested arrays and all. (For data other programs must read, JSON is the usual choice: see JSON.)

ExampleRun it in the playground
put "Ada" into tRecord["name"]
put 36 into tRecord["age"]
put arrayEncode(tRecord) into tData
put arrayDecode(tData) into tBack
put tBack["name"] && tBack["age"]
Output
Ada 36

Try it yourself

Read the settings width=640;height=480;title=Demo into an array, change the title to My game, and turn the array back into the same kind of text, with its keys in alphabetical order.

Show a solution
ExampleRun it in the playground
put "width=640;height=480;title=Demo" into tSettings
split tSettings by ";" and "="
put "My game" into tSettings["title"]
combine tSettings by return and "="
sort tSettings
replace return with ";" in tSettings
put tSettings
Output
height=480;title=My game;width=640

Words in this lesson

Each one finds every lesson that uses it. The Dictionary has the whole story of each, with its syntax and more examples.