digitalmars.D - Associative Array Problems
I read about Associative Arrays (which remind me of hashes from Ruby/Perl), and wanted to do a test... and failed. I'm using this: char[char[]] [] my_aa; To try to make an associative array that I can use like this: my_aa["hello"] = "Hello, world!"; However, when I compile, I get this: cannot implicitly convert char[5] to int How can I declare the variable the correct way? I guess that I need to make an associative array of strings.
Jun 19 2004
On Sun, 20 Jun 2004 05:49:41 +0000 (UTC), kinghajj wrote:char[char[]] [] my_aa; To try to make an associative array that I can use like this: my_aa["hello"] = "Hello, world!";Very close. Try this instead... char[][char[]] my_aa; The format is ... <datatype>[char[]] <idname>; and in your case the <datatype> is 'char[]'. What you had declared was a dynamic array of associative arrays of char. -- Derek Melbourne, Australia
Jun 19 2004
In article <7mccg3fla2ax$.flas6levjw10.dlg 40tude.net>, Derek says...Very close. Try this instead... char[][char[]] my_aa; The format is ... <datatype>[char[]] <idname>;Oh! OK, that makes sense now! They should add that to the documentation on arrays. Quick follow up, can you use associative arrays like this: char[] key = "test"; char[][char[]] my_aa; my_aa[key] = "Hello"; printf("MSG: %.*s\n", my_aa["test"]);and in your case the <datatype> is 'char[]'. What you had declared was a dynamic array of associative arrays of char. -- Derek Melbourne, Australia
Jun 19 2004
On Sun, 20 Jun 2004 06:16:30 +0000 (UTC), kinghajj wrote:In article <7mccg3fla2ax$.flas6levjw10.dlg 40tude.net>, Derek says...This works for me... char[][char[]] my_aa; char[] key = "hello"; void main() { my_aa[key] = "Hello, world!"; printf("%.*s", my_aa[key]); } -- Derek Melbourne, AustraliaVery close. Try this instead... char[][char[]] my_aa; The format is ... <datatype>[char[]] <idname>;Oh! OK, that makes sense now! They should add that to the documentation on arrays. Quick follow up, can you use associative arrays like this: char[] key = "test"; char[][char[]] my_aa; my_aa[key] = "Hello"; printf("MSG: %.*s\n", my_aa["test"]);
Jun 20 2004