Associative array/Creation: Difference between revisions

Content added Content deleted
m (→‎[[Creating_an_Associative_Array#ALGOL 68]]: not sure if this is called widening or rowing or something else.)
Line 62: Line 62:


MODE COLOR = BITS;
MODE COLOR = BITS;
FORMAT color repr = $"16r"16r6d$;


# This is an associative array which maps strings to ints #
# This is an associative array which maps strings to ints #
Line 101: Line 102:
# then, get some values out #
# then, get some values out #
COLOR color; color := color map("green"); # color gets 16r00ff00 #
COLOR color; color := color map("green"); # color gets 16r00ff00 #
color := color map("black"); # accessing unassigned values assigns them to 16r0 #
color := color map("black"); # accessing unassigned values assigns them to 0 #
# get some value out without accidentally inserting new ones #
# get some value out without accidentally inserting new ones #
Line 108: Line 109:
put(stand error, ("color not found!", new line))
put(stand error, ("color not found!", new line))
ELSE
ELSE
printf(($"green: 16r"16r6dl$, value))
printf(($"green: "f(color repr)l$, value))
FI;
FI;
Line 117: Line 118:
FOR index FROM LWB color map items TO UPB color map items DO
FOR index FROM LWB color map items TO UPB color map items DO
ITEM item = color map items[index];
ITEM item = color map items[index];
putf(stand error, ($"color map("""g""") = 16r"16r6dl$, key OF item, value OF item))
putf(stand error, ($"color map("""g""") = "f(color repr)l$, item))
OD;
OD;


FORMAT fmt;
FORMAT comma sep = $"("n(UPB color map items-1)(f(fmt)",")f(fmt)")"$;
FORMAT fmt := $""""g""""$;
FORMAT comma sep = $"("n(UPB color map items-1)(f(fmt)", ")f(fmt)")"$;

fmt := $""""g""""$;
printf(($g$,"keys: ", comma sep, key OF color map items, $l$));
printf(($g$,"keys: ", comma sep, key OF color map items, $l$));
fmt := $"16r"16r6d$;
fmt := color repr;
printf(($g$,"values: ", comma sep, value OF color map items, $l$))
printf(($g$,"values: ", comma sep, value OF color map items, $l$))


Line 136: Line 139:
color map("my favourite color") = 16r337733
color map("my favourite color") = 16r337733
color map("black") = 16r000000
color map("black") = 16r000000
keys: ("red","green","blue","my favourite color","black")
keys: ("red", "green", "blue", "my favourite color", "black")
values: (16rff0000,16r00ff00,16r0000ff,16r337733,16r000000)
values: (16rff0000, 16r00ff00, 16r0000ff, 16r337733, 16r000000)
</pre>
</pre>