Task

The goal is to create an associative array (also known as a dictionary, map, or hash).



Related tasks:

 Associative arrays/Iteration
 Hash from two arrays


See also

 Array
 Associative array: Creation, Iteration
 Collections
 Compound data type
 Doubly-linked list: Definition, Element definition, Element insertion, List Traversal, Element Removal
 Linked list
 Queue: Definition, Usage
 Set
 Singly-linked list: Element definition, Element insertion, List Traversal, Element Removal
 Stack


include c:\cxpl\stdlib;
char Dict(10,10);
int  Entries;
 
proc AddEntry(Letter, Greek);   \Insert entry into associative array
char Letter, Greek;
[Dict(Entries,0):= Letter;
StrCopy(Greek, @Dict(Entries,1));
Entries:= Entries+1;            \(limit checks ignored for simplicity)
];
 
func Lookup(Greek);             \Given Greek name return English letter
char Greek;
int  I;
[for I:= 0, Entries-1 do
    if StrCmp(Greek, @Dict(I,1)) = 0 then return Dict(I,0);
return ^?;
];
 
[Entries:= 0;
AddEntry(^A, "alpha");
AddEntry(^D, "delta");
AddEntry(^B, "beta");
AddEntry(^C, "gamma");
ChOut(0, Lookup("beta"));  CrLf(0);
ChOut(0, Lookup("omega")); CrLf(0);
]