FOREACH KEY

The statement FOREACH KEY enumerates all keys in a MAP variable and runs the script part for each key.

The following is a definition of the statement.

FOREACH KEY key IN map
DO
    <Template scripting part with declarations>
OD

FOREACH KEY key IN SORTED map
DO
    <Template scripting part with declarations>
OD

The enumeration variable (key) of type TEXT must be declared before the FOREACH loop. The map can be any type of MAP. You can use the keyword SORTED to enumerate the keys in a alphabetically sorted order. If this keyword is omitted, the ordering of the enumeration is undefined. Changes to the map in the DO … OD section do not affect the enumeration.

The following is an example of the statement.

# BEGIN
MAP NUMBER example
ASSIGN example["one"] := 1
ASSIGN example["two"] := 2
ASSIGN example["three"] := 3
ASSIGN example["four"] := 4
TEXT key

FOREACH KEY key IN SORTED example
DO

#
@(key)      @(number(example[key];0))
#

    ASSIGN example["magic"] := 42
OD

#
Show changes: Element 'magic' = @(number(example["magic"];0))
#

END #

Result:
four      4
one       1
three     3
two       2
Show changes: Element 'magic' = 42

The 'magic' element is not part of the enumeration because it is introduced in the DO...OD part.