www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Associative Array Problems

reply kinghajj <kinghajj_member pathlink.com> writes:
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
parent reply Derek <derek psyc.ward> writes:
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
parent reply kinghajj <kinghajj_member pathlink.com> writes:
In article <7mccg3fla2ax$.flas6levjw10.dlg 40tude.net>, Derek says...
Very close. Try this instead...

  char[][char[]] my_aa;


The format is ...
  <datatype>[char[]] <idname>;

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
parent Derek <derek psyc.ward> writes:
On Sun, 20 Jun 2004 06:16:30 +0000 (UTC), kinghajj wrote:

 In article <7mccg3fla2ax$.flas6levjw10.dlg 40tude.net>, Derek says...
Very close. Try this instead...

  char[][char[]] my_aa;


The format is ...
  <datatype>[char[]] <idname>;

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"]);

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, Australia
Jun 20 2004