digitalmars.D.learn - Associative array mapping strings to strings
- Andrew Madigan (23/23) Apr 14 2006 There was a post about this earlier, but I can't seem to find an answer....
- Derek Parnell (11/37) Apr 14 2006 The .keys in this case is a list of strings not a list of chars. So use ...
- Andrew Madigan (3/53) Apr 14 2006 Yeah, I just realized that as well. Thank you for your quick response. T...
- Jarrett Billingsley (9/15) Apr 15 2006 Of course, this isn't the most efficient way to iterate through an AA, a...
- Andrew Madigan (6/25) Apr 15 2006 Thank you, I was wondering what the syntax was for that. My main languag...
- Chris Nicholson-Sauls (7/39) Apr 15 2006 Just to take this a little further, the "Ultimately D-ish Version" would...
- Jarrett Billingsley (5/10) Apr 15 2006 You and your dirty type inference. ;)
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
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
Derek Parnell wrote:On Sat, 15 Apr 2006 12:41:45 +1000, Andrew Madigan <amadigan gmail.com> wrote:Yeah, I just realized that as well. Thank you for your quick response. The code works fine now. Thanks again.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]]); }
Apr 14 2006
"Andrew Madigan" <amadigan gmail.com> wrote in message news:e1pnqr$rh4$1 digitaldaemon.com...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.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 15 2006
Jarrett Billingsley wrote:"Andrew Madigan" <amadigan gmail.com> wrote in message news:e1pnqr$rh4$1 digitaldaemon.com...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.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.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 15 2006
Andrew Madigan wrote:Jarrett Billingsley wrote:Just to take this a little further, the "Ultimately D-ish Version" would be: Type inference, raw string, typed string, and formatting safety. -- Chris Nicholson-Sauls"Andrew Madigan" <amadigan gmail.com> wrote in message news:e1pnqr$rh4$1 digitaldaemon.com...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.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.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 15 2006
"Chris Nicholson-Sauls" <ibisbasenji gmail.com> wrote in message news:e1qdm4$1l8i$1 digitaldaemon.com...Type inferenceYou 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