Associative array mapping strings to strings

Derek Parnell derek at psych.ward
Fri Apr 14 19:49:16 PDT 2006


On Sat, 15 Apr 2006 12:41:45 +1000, Andrew Madigan <amadigan at gmail.com>  
wrote:

> There was a post about this earlier, but I can't seem to find an answer.  
> How
> do I create an associate array mapping strings to strings?
>
> I've tried:
>
> char[][char[]] strings;
>
> ----------------
>
> alias char[] string;
>
> string[string] strings;
>
> Which are of course equivalent. Putting strings into the array is not a
> problem, however, when I try to retrieve elements I get the following
> compiler errors:
>
> (Code:)
>
> char[] headerNames = strings.keys;
> for (int i = 0; i < headerNames.length; i++)    {
>         writefln("%s: %s", headerNames[i], strings[headerNames[i]]);
> }
>
> (Errors:)
>
> TestApp.d:9: cannot implicitly convert expression (_aaKeys(strings,8)) of
> type char[][] to char[]
> TestApp.d:11: cannot implicitly convert expression (headerNames[i]) of  
> type
> char to char[]
>
> This is gdc 0.17, based on dmd 0.14. I'm trying to compile the 0.18 alpha
> but it's failing (I'll post about that on the appropriate forum). Is  
> there
> something wrong with the declaration? Is there a declaration that works?

The .keys in this case is a list of strings not a list of chars. So use  
this ...

  char[][] headerNames = strings.keys;
  for (int i = 0; i < headerNames.length; i++)    {
          writefln("%s: %s", headerNames[i], strings[headerNames[i]]);
  }


-- 
Derek Parnell
Melbourne, Australia



More information about the Digitalmars-d-learn mailing list