Associative array mapping strings to strings

Chris Nicholson-Sauls ibisbasenji at gmail.com
Sat Apr 15 02:21:06 PDT 2006


Andrew Madigan wrote:
> Jarrett Billingsley wrote:
> 
> 
>>"Andrew Madigan" <amadigan at gmail.com> wrote in message
>>news:e1pnqr$rh4$1 at 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



More information about the Digitalmars-d-learn mailing list