Skip to content

Commit da7fa7a

Browse files
authored
Add example of associative array (#4199)
Add another example of string lookup using associative array. It allows to introduce AA type that a pure C developer would not expect as a builtin in language.
1 parent 7c4fcf7 commit da7fa7a

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

articles/ctod.dd

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,23 @@ void dostring(string s)
637637
to generate a fast lookup scheme for it, eliminating the bugs
638638
and time required in hand-coding one.
639639

640+
Another possibility of associating a string to a number is to
641+
use an associative array.
642+
643+
$(RUNNABLE_EXAMPLE_COMPILE
644+
----------------------------
645+
ulong[string] map = ["hello" : 1616515,
646+
"goodbye" : 42,
647+
"maybe" : ulong.max
648+
];
649+
string s = "goodbye";
650+
651+
writeln("number is ", map[s]);
652+
----------------------------
653+
)
654+
655+
Any type can be used as key or as value.
656+
640657
<hr>$(COMMENT ============================================ )
641658
$(H2 $(LNAME2 align, Setting struct member alignment))
642659

0 commit comments

Comments
 (0)