Array with char-keys

Pragma ericanderton at yahoo.removeme.com
Thu May 24 13:44:50 PDT 2007


Charma wrote:
> Hello,
> i am very new with D (i do know C++ though) so i have a maybe strange question. Is there any way i can make an array which has char-type keys? Like in php in which something like that is possible:
> array{
>     "textbla" => value,
>     "anotherText" => value,
>     and so on...
> }
> Or maybe there is another way so i can values in an array with char-strings?
> what i want is to save an unsorted file-list in an array, in a way so that i don't need to look through the whole array to find the file.
> 
> Any Idea's?
> 
> Thanks

What you want is an associative array:

char[char[]] myMap;

myMap["one"] = "first";
myMap["two"] = "second";
myMap["three"] = "third";

http://www.digitalmars.com/d/arrays.html#associative

Traversal is also very easy with this type:

foreach(key,value; myMap){
   writefln("%s => %s",key,value);
}

Tests are done with the 'in' operator:

if("two" in myMap){
   writefln("mymap contains a value for 'two'");
}

-- 
- EricAnderton at yahoo



More information about the Digitalmars-d mailing list