PushMapPosition

Map command

Syntax

Remembers the current entry of a map on a stack, so that PopMapPosition() can come back to it.

PushMapPosition(<map>())

Parts

<map>The map, written with its empty parentheses: stock(). It must be a NewMap variable or a map field of a record.

Good to know

  • Each map has its own stack. Pushes and pops pair up: the last position pushed is the first one popped.
  • When there is no current entry, that state is remembered: PopMapPosition() then leaves the map with no current entry.
  • Use the pair around a lookup inside a FOREACH, which would otherwise move the loop's cursor.

Example

NewMap price.i()price("apple") = 3price("pear") = 5price("plum") = 2FOREACH price()  PushMapPosition(price())  FindMapElement(price(), "pear")  best = price()  PopMapPosition(price())  Console(MapKey(price()) + " " + Str(best - price()))   ' apple 2, pear 0, plum 3NEXT
In the manual : Arrays, lists and maps