Lists and maps
Besides arrays, x has two built-in collections:
- a list holds elements in order, and grows and shrinks one element at a time;
- a map finds elements by a string key (a hash table, or dictionary).
Both can hold numbers, strings or structures. Each has a current element, which is what name() refers to.
Lists
NewList name.type() creates an empty list. AddElement adds an element after the current one and makes it current, so name() then reads or writes it:
NewList names.s()
AddElement(names()) : names() = "Tom"
AddElement(names()) : names() = "Dick"
AddElement(names()) : names() = "Harry"
OpenConsole()
ForEach names()
PrintN(Str(ListIndex(names())) + ": " + names())
Next
PrintN(Str(ListSize(names())) + " names")
Output:
0: Tom
1: Dick
2: Harry
3 names
ForEach … Next visits every element in order, making each one current.
A list of structures reaches fields through name()\field:
Structure Point
x.i
y.i
EndStructure
NewList points.Point()
For i = 1 To 3
AddElement(points())
points()\x = i
points()\y = i * i
Next
OpenConsole()
total = 0
ForEach points()
total + points()\x * points()\y
Next
PrintN(Str(total))
Output:
36
Moving around and deleting
NewList n.i()
For i = 1 To 5
AddElement(n()) : n() = i * 10
Next
OpenConsole()
FirstElement(n())
PrintN(Str(n()))
LastElement(n())
PrintN(Str(n()))
SelectElement(n(), 2)
PrintN(Str(n()))
DeleteElement(n())
PrintN(Str(n()) + " " + Str(ListSize(n())))
ForEach n()
If n() = 40
DeleteElement(n())
EndIf
Next
out$ = ""
ForEach n()
out$ + Str(n()) + " "
Next
PrintN(Trim(out$))
Output:
10
50
30
20 4
10 20 50
After DeleteElement, the element before the deleted one becomes current. That is what makes deleting inside ForEach safe: the loop carries on with the element after the deleted one.
See the List library for every function.
Maps
NewMap name.type() creates an empty map. name("key") reads or writes the element with that key, and creates it if it doesn't exist yet:
NewMap capital.s()
capital("FR") = "Paris"
capital("DE") = "Berlin"
capital("UK") = "London"
OpenConsole()
PrintN(capital("DE"))
ForEach capital()
PrintN(MapKey(capital()) + " = " + capital())
Next
PrintN(Str(MapSize(capital())))
Output:
Berlin
FR = Paris
DE = Berlin
UK = London
3
ForEach visits map elements in the order they were added. (PureBasic doesn't define an order. x does, so programs behave the same on every run.)
To check for a key without creating it, use FindMapElement:
NewMap stock.i()
stock("apples") = 5
stock("pears") = 2
stock("apples") + 3
OpenConsole()
If FindMapElement(stock(), "pears")
PrintN("pears: " + Str(stock()))
EndIf
If FindMapElement(stock(), "plums") = 0
PrintN("no plums")
EndIf
DeleteMapElement(stock(), "pears")
PrintN(Str(stock("apples")) + " " + Str(MapSize(stock())))
Output:
pears: 2
no plums
8 1
Keys are compared exactly, so "a" and "A" are different keys.
A typical use, counting words:
text$ = "the cat and the hat and the bat"
NewMap count.i()
For i = 1 To CountString(text$, " ") + 1
count(StringField(text$, i, " ")) + 1
Next
OpenConsole()
ForEach count()
PrintN(MapKey(count()) + " " + Str(count()))
Next
Output:
the 3
cat 1
and 2
hat 1
bat 1
See the Map library for every function.
Passing to procedures
List name.type() and Map name.type() parameters pass a collection by reference:
Procedure.i Sum(List values.i())
Protected total
ForEach values()
total + values()
Next
ProcedureReturn total
EndProcedure
NewList nums.i()
For i = 1 To 4
AddElement(nums()) : nums() = i
Next
OpenConsole()
PrintN(Str(Sum(nums())))
Output:
10
Scope and lifetime
A list or map created in main code is visible only there, like other main-code variables, unless it is declared Global (Global NewList …) or imported with Shared.
One created inside a procedure belongs to that call, and is freed, with all its elements, when the procedure returns:
Procedure.s Joined(n)
NewList parts.s()
For i = 1 To n
AddElement(parts()) : parts() = Str(i)
Next
ForEach parts()
out$ + parts()
Next
ProcedureReturn out$
EndProcedure
OpenConsole()
PrintN(Joined(5))
Output:
12345
Running NewList again on an existing list empties it.
Not yet available 🔜
SortList,SortStructuredList,CopyList,CopyMap,MergeLists,SplitList,MoveElement,SwapElements.PushListPosition/PopListPosition,PushMapPosition/PopMapPosition.ChangeCurrentElement,FreeList,FreeMap.- Lists, maps and arrays as structure fields.