Values with names
An array is a variable that holds many values, each filed under a key. You write the key in square brackets after the variable's name. A key can be any text or number; the value stored under it is an element.
put "Ada" into tPerson["name"]
put 36 into tPerson["age"]
put "London" into tPerson["town"]
put tPerson["name"] && "is" && tPerson["age"]
put the number of elements of tPerson
put tPerson is an array
Ada is 36 3 true
An array is not text, so put tPerson on its own shows nothing: ask for its elements, or turn it into text first, as the next lesson shows.
Which keys are there?
the keys of an array is a list of its keys, a line each. The keys come in no particular order, so sort them when the order matters:
put "Ada" into tPerson["name"]
put 36 into tPerson["age"]
put "London" into tPerson["town"]
put the keys of tPerson into tKeys
sort tKeys
put tKeys
put "age" is among the lines of the keys of tPerson
age name town true
Going through an array
repeat for each key (Repeat for each) visits every key, and repeat for each element every value. delete variable with a key removes that element:
put 5 into tStock["apples"]
put 0 into tStock["pears"]
put 12 into tStock["figs"]
repeat for each key tFruit in tStock
if tStock[tFruit] is 0 then put tFruit && "are sold out"
end repeat
delete variable tStock["pears"]
put the number of elements of tStock
repeat for each element tCount in tStock
add tCount to tTotal
end repeat
put tTotal && "pieces of fruit"
pears are sold out 2 17 pieces of fruit
Counting with an array
Arrays shine when you need to count things. Use each thing as a key and add 1 to its element: an element that does not exist yet starts as empty, which add treats as 0.
put "the cat and the dog and the bird" into tText
repeat for each word tWord in tText
add 1 to tCount[tWord]
end repeat
put the keys of tCount into tWords
sort tWords
repeat for each line tWord in tWords
put tWord & ":" && tCount[tWord]
end repeat
and: 2 bird: 1 cat: 1 dog: 1 the: 3
Numbered keys
Keys that are 1, 2, 3 make an array a numbered list, and a loop with a counter walks it in order:
put "red" into tColours[1]
put "green" into tColours[2]
put "blue" into tColours[3]
repeat with i = 1 to the number of elements of tColours
put i & "." && tColours[i]
end repeat
1. red 2. green 3. blue
Arrays inside arrays
An element can itself be an array, which gives you records and tables. Here a numbered array holds a record for each person:
put "Ada" into tPeople[1]["name"]
put 36 into tPeople[1]["age"]
put "Alan" into tPeople[2]["name"]
put 41 into tPeople[2]["age"]
put the number of elements of tPeople
repeat with i = 1 to the number of elements of tPeople
put tPeople[i]["name"] && "is" && tPeople[i]["age"]
end repeat
put tPeople[2] is an array
2 Ada is 36 Alan is 41 true
Arrays in and out of handlers
A whole array can be passed to a handler and returned from one, which is a tidy way to give back several named values at once:
function newPoint pX, pY
put pX into tPoint["x"]
put pY into tPoint["y"]
return tPoint
end newPoint
function distanceFromOrigin pPoint
return sqrt(pPoint["x"] ^ 2 + pPoint["y"] ^ 2)
end distanceFromOrigin
put newPoint(3, 4) into tPoint
put tPoint["x"] & "," & tPoint["y"]
put distanceFromOrigin(tPoint)
3,4 5
Arrays are copied when you put them into another variable or pass them, like any value: changing the copy leaves the original alone.
put 5 into tOriginal["score"]
put tOriginal into tCopy
put 9 into tCopy["score"]
put tOriginal["score"] && tCopy["score"]
5 9
Keys and capitals
Keys ignore case, like the rest of xTalk, unless caseSensitive is true:
put 1 into tA["Apple"]
put tA["APPLE"]
set the caseSensitive to true
put "[" & tA["APPLE"] & "]"
1 []
Try it yourself
Count how many times each letter appears in mississippi, and put each letter that appears with its count, in alphabetical order, like i 4.
Show a solution
repeat for each char tLetter in "mississippi"
add 1 to tCount[tLetter]
end repeat
put the keys of tCount into tLetters
sort tLetters
repeat for each line tLetter in tLetters
put tLetter && tCount[tLetter]
end repeat
i 4 m 1 p 2 s 4
Words in this lesson
arraykeyelementthe keysthe number of elementsrepeat for each keyrepeat for each elementdelete variableis an arrayEach one finds every lesson that uses it. The Dictionary has the whole story of each, with its syntax and more examples.