Thread overview
Array with char-keys
May 24, 2007
Charma
May 24, 2007
BCS
May 24, 2007
Pragma
May 24, 2007
Charma
May 24, 2007
Regan Heath
May 24, 2007
Bill Baxter
May 25, 2007
pragma
May 24, 2007
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
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
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
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
Pragma Wrote:
> What you want is an associative array:
> 
> char[char[]] myMap;

minor correction:

char[] [char[]] myMap;

Regan

May 24, 2007
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 25, 2007
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.