www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Array with char-keys

reply Charma <Motoko_Kusanagi web.de> writes:
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
May 24 2007
next sibling parent BCS <ao pathlink.com> writes:
Reply to Charma,

 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
 

there are assortative arrays: int[ char[] ] arr; this will allow a string to be used as a key into a mapping (it's a hash under the hood). Is that what you want?
May 24 2007
prev sibling parent reply Pragma <ericanderton yahoo.removeme.com> writes:
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
May 24 2007
next sibling parent Charma <Motoko_Kusanagi web.de> writes:
yes! thank you both very much! i am right away to try it. this is a great
forum... my answers are allways answered quick and good.

I will write again if there are any problem.. thanks again ^^;;
May 24 2007
prev sibling parent reply Regan Heath <regan netmail.co.nz> writes:
Pragma Wrote:
 What you want is an associative array:
 
 char[char[]] myMap;

minor correction: char[] [char[]] myMap; Regan
May 24 2007
next sibling parent Bill Baxter <dnewsgroup billbaxter.com> writes:
Regan Heath wrote:
 Pragma Wrote:
 What you want is an associative array:

 char[char[]] myMap;

minor correction: char[] [char[]] myMap; Regan

Which is a mistake that's a lot harder to make with a little alias magic. Easier to read the intent as well. alias char[] string; string[string] myMap; --bb
May 24 2007
prev sibling parent pragma <ericanderton yahoo.com> writes:
Regan Heath wrote:
 Pragma Wrote:
 What you want is an associative array:

 char[char[]] myMap;

minor correction: char[] [char[]] myMap; Regan

GAh! Wow, note to self: do not post without coffee.
May 24 2007