FindMapElement

Map command

Syntax

Looks for a key without creating it. When the key exists, its entry becomes the current one.

FindMapElement(<map>(), <key>)

Parts

<map>The map, written with its empty parentheses: stock(). It must be a NewMap variable or a map field of a record.
<key>The key to look for, turned into text. The search is case-sensitive.

Returns

1 when the key exists, 0 when it does not.

Good to know

  • This is the way to test a key. Reading m("key") creates the entry with the default value when it is missing.
  • When the key is missing, the map is left with no current entry: MapKey() returns "" and reading m() stops the program with "BASIC map has no current element".
  • Inside a FOREACH over the same map, FindMapElement moves the loop's cursor. Wrap it in PushMapPosition() and PopMapPosition().

Example

NewMap stock.i()stock("apple") = 3IF FindMapElement(stock(), "apple") THEN Console(Str(stock()))   ' 3IF FindMapElement(stock(), "plum") = 0 THEN Console("no plum")   ' no plumConsole(Str(MapSize(stock())))   ' 1
In the manual : Arrays, lists and maps