LessonsIntermediate: Stacks, objects and dataLesson 32

Arrays

Keep many named values in one variable, look them up by key, count with them, and build records and tables out of them.

  • Intermediate
  • Lesson 32 of 83
  • About 10 minutes
  • 10 examples

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.

ExampleRun it in the playground
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
Output
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:

ExampleRun it in the playground
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
Output
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:

ExampleRun it in the playground
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"
Output
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.

ExampleRun it in the playground
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
Output
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:

ExampleRun it in the playground
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
Output
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:

ExampleRun it in the playground
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
Output
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:

ExampleRun it in the playground
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)
Output
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.

ExampleRun it in the playground
put 5 into tOriginal["score"]
put tOriginal into tCopy
put 9 into tCopy["score"]
put tOriginal["score"] && tCopy["score"]
Output
5 9

Keys and capitals

Keys ignore case, like the rest of xTalk, unless caseSensitive is true:

ExampleRun it in the playground
put 1 into tA["Apple"]
put tA["APPLE"]
set the caseSensitive to true
put "[" & tA["APPLE"] & "]"
Output
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
ExampleRun it in the playground
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
Output
i 4
m 1
p 2
s 4

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.