Syntax
Declares a map: values stored under text keys. <name>(<key>) reads or writes one entry, <name>() the current one.
NewMap <name>.<type>([<slots>])
NewMap <name>([<slots>])
GLOBAL NewMap <name>.<type>([<slots>])
LOCAL NewMap <name>.<type>([<slots>])
<name>(<key>)
<name>()
<name>(<key>).<field>Parts
| <name> | The map's name, with no sigil. It cannot also be the name of a list. |
|---|---|
| <type> | The type of the values, as for NewList: a short suffix, a full type name, or a TYPE name. Without it, the map holds integers. |
| <slots> | Optional size hint kept for PureBasic programs. It must be a positive whole number (0 or less stops the program) and changes nothing else. |
| <key> | The key, as text. A number is turned into text: m(1) and m("1") are the same entry. Keys are case-sensitive. |
| <field> | A field of the entry, for a map of records: bag("sword").qty = 1. |
Good to know
m("key") = vcreates the entry or replaces its value.+=,-=,*=and/=work on an entry.- Reading
m("key")creates a missing entry with the default value. To test without creating, use FindMapElement(). - Every access makes that entry the current one. With no current entry, reading
m()stops the program:BASIC map has no current element. NewMap m$()is refused at the first use ofm(). Write the type after a dot:NewMap m.s().- In a program with a
DO … LOOP, a FUNCTION does not see a map declared aboveDO(… is not defined). Declare it withGLOBAL NewMap.
Example
NewMap stock.i()stock("apple") = 3stock("pear") = 5stock("apple") += 2Console(Str(stock("apple")) + " " + Str(MapSize(stock()))) ' 5 2IF FindMapElement(stock(), "plum") = 0 THEN Console("no plum") ' no plumConsole(Str(stock("plum")) + " " + Str(MapSize(stock()))) ' 0 3In the manual : Arrays, lists and maps