www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Associative array mapping strings to strings

reply Andrew Madigan <amadigan gmail.com> writes:
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?
Apr 14 2006
parent reply "Derek Parnell" <derek psych.ward> writes:
On Sat, 15 Apr 2006 12:41:45 +1000, Andrew Madigan <amadigan 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
Apr 14 2006
parent reply Andrew Madigan <amadigan gmail.com> writes:
Derek Parnell wrote:

 On Sat, 15 Apr 2006 12:41:45 +1000, Andrew Madigan <amadigan 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]]); }

Yeah, I just realized that as well. Thank you for your quick response. The code works fine now. Thanks again.
Apr 14 2006
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Andrew Madigan" <amadigan gmail.com> wrote in message 
news:e1pnqr$rh4$1 digitaldaemon.com...
   char[][] headerNames = strings.keys;
   for (int i = 0; i < headerNames.length; i++)    {
           writefln("%s: %s", headerNames[i], strings[headerNames[i]]);
   }

code works fine now. Thanks again.

Of course, this isn't the most efficient way to iterate through an AA, as it has to dynamically create the keys array, and then iterate through it. It's better to do foreach(char[] key, char[] value; strings) writefln(key, ": ", value); This traverses the AA directly, without allocating memory for the keys array.
Apr 15 2006
parent reply Andrew Madigan <amadigan gmail.com> writes:
Jarrett Billingsley wrote:

 "Andrew Madigan" <amadigan gmail.com> wrote in message
 news:e1pnqr$rh4$1 digitaldaemon.com...
   char[][] headerNames = strings.keys;
   for (int i = 0; i < headerNames.length; i++)    {
           writefln("%s: %s", headerNames[i], strings[headerNames[i]]);
   }

The code works fine now. Thanks again.

Of course, this isn't the most efficient way to iterate through an AA, as it has to dynamically create the keys array, and then iterate through it. It's better to do foreach(char[] key, char[] value; strings) writefln(key, ": ", value); This traverses the AA directly, without allocating memory for the keys array.

Thank you, I was wondering what the syntax was for that. My main language is Java, I didn't realize it was that easy. A general thought on D: Given the power and features of the language, and assuming projects such as wxd and Ares are successful, it could become what Java was supposed to be, it's already a lot better. I'm really impressed.
Apr 15 2006
parent reply Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
Andrew Madigan wrote:
 Jarrett Billingsley wrote:
 
 
"Andrew Madigan" <amadigan gmail.com> wrote in message
news:e1pnqr$rh4$1 digitaldaemon.com...

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

Yeah, I just realized that as well. Thank you for your quick response. The code works fine now. Thanks again.

Of course, this isn't the most efficient way to iterate through an AA, as it has to dynamically create the keys array, and then iterate through it. It's better to do foreach(char[] key, char[] value; strings) writefln(key, ": ", value); This traverses the AA directly, without allocating memory for the keys array.

Thank you, I was wondering what the syntax was for that. My main language is Java, I didn't realize it was that easy. A general thought on D: Given the power and features of the language, and assuming projects such as wxd and Ares are successful, it could become what Java was supposed to be, it's already a lot better. I'm really impressed.

Just to take this a little further, the "Ultimately D-ish Version" would be: # foreach (key, value; strings) { # writefln(`%s: %s`c, key, value); # } Type inference, raw string, typed string, and formatting safety. -- Chris Nicholson-Sauls
Apr 15 2006
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Chris Nicholson-Sauls" <ibisbasenji gmail.com> wrote in message 
news:e1qdm4$1l8i$1 digitaldaemon.com...
 # foreach (key, value; strings) {
 #   writefln(`%s: %s`c, key, value);
 # }

 Type inference

You and your dirty type inference. ;)
 formatting safety.

Oh! Yeah, I always forget to do that with strings. Then I end up with a format exception and wonder why.
Apr 15 2006