Map

Functions for maps created with NewMap. See Lists and maps for an introduction.

A map is passed to these functions with empty brackets: MapSize(scores()). Elements are visited in the order they were added.

AddMapElement ✅

*Result = AddMapElement(Map(), Key$ [, Flags])

Adds a new empty element with the key, replacing any element that already has that key. The new element becomes current. Usually not needed: map("key") = value adds the element itself.

Returns the address of the new element.

FindMapElement ✅

*Result = FindMapElement(Map(), Key$)

Looks for the element with the key, without creating it. If it is found, it becomes current.

Returns its address, or 0 if there is no element with that key.

DeleteMapElement ✅

Result = DeleteMapElement(Map() [, Key$])

Deletes the element with the key, or the current element if no key is given. If the current element is deleted, the element added before it becomes current, so deleting inside ForEach is safe.

Returns nonzero if an element was deleted.

ClearMap ✅

ClearMap(Map())

Deletes every element.

MapSize ✅

Result = MapSize(Map())

The number of elements.

MapKey ✅

Key$ = MapKey(Map())

The key of the current element.

ResetMap ✅

ResetMap(Map())

Makes no element current, so the next NextMapElement goes to the first element.

NextMapElement ✅

*Result = NextMapElement(Map())

Moves to the next element. Returns its address, or 0 after the last one.

NewMap age.i()
age("Ada") = 36
age("Alan") = 41
AddMapElement(age(), "Grace")
age() = 85

OpenConsole()
ResetMap(age())
While NextMapElement(age())
  PrintN(MapKey(age()) + " " + Str(age()))
Wend
DeleteMapElement(age(), "Alan")
ClearMap(age())
PrintN(Str(MapSize(age())))

Output:

Ada 36
Alan 41
Grace 85
0

Notes. NewMap name.type(Slots) sets the starting number of hash slots (512 by default). x adds slots as a map grows, so the number only matters for speed with very large maps.

CopyMap ✅

Result = CopyMap(SourceMap(), DestinationMap())

Makes DestinationMap() a copy of SourceMap() (the same element type), as CopyList does for lists. Returns non-zero.

CompareMap ✅

Result = CompareMap(Map1(), Map2() [, Flags])

Returns non-zero if the two maps have the same keys, each with an equal element (compared as CompareList compares elements), in any order.

OpenConsole()
NewMap price.d() : NewMap copy.d()
price("tea") = 1.5 : price("cake") = 2.25
CopyMap(price(), copy())
PrintN(Str(MapSize(copy())) + " " + StrD(copy("cake")) + " " + Str(CompareMap(price(), copy())))
copy("tea") = 1.75
PrintN(Str(CompareMap(price(), copy())))

Output:

2 2.25 1
0

PushMapPosition, PopMapPosition ✅

PushMapPosition(Map())
*Element = PopMapPosition(Map())

Remembers which element is current, and makes it current again, as for lists.

FreeMap ✅

FreeMap(Map())

Frees all the elements (in x the map is then empty).