www.digitalmars.com         C & C++   DMDScript  

D - Assoc array bug?

reply Patrick Down <pat codemoon.com> writes:
What I want is an associative array of char[] to char[]

The following program generates this compiler error:
test.d(9): cannot implicitly convert wchar[3] to int
Line 9 is map["str"] = "str";

int main(char[][] args)
{    
  char[][char[]] map;
    
  map["str"] = "str"; 
    
  return 0;
}

This little work around fixes the problem.

alias char[] string;

int main(char[][] args)
{    
  string[char[]] map;
    
  map["str"] = "str";
    
  return 0;
}
May 09 2002
next sibling parent reply "Walter" <walter digitalmars.com> writes:
Thanks!

"Patrick Down" <pat codemoon.com> wrote in message
news:Xns9209D80E1BC4Bpatcodemooncom 63.105.9.61...
 What I want is an associative array of char[] to char[]

 The following program generates this compiler error:
 test.d(9): cannot implicitly convert wchar[3] to int
 Line 9 is map["str"] = "str";

 int main(char[][] args)
 {
   char[][char[]] map;

   map["str"] = "str";

   return 0;
 }

 This little work around fixes the problem.

 alias char[] string;

 int main(char[][] args)
 {
   string[char[]] map;

   map["str"] = "str";

   return 0;
 }

May 09 2002
parent Patrick Down <pat codemoon.com> writes:
How to read arrays. :-)


elem_type[] A;

One way to "read" this is: A is a dynamic array of type elem_type

elem_type[5] A;

A is a dynamic array of length 5 of type elem_type

elem_type[5][6] A;

A is an array of length 5 of type (array of length 6 of type 
elem_type).

elem_type[key_type] A;

A is an associtive array that maps a key of type key_type 
to a value type of elem_type.

elem_type[key_elem_type[]][] A;

A is an associative array that maps a key of type (dynamic 
array of type key_elem_type) to a value type of (dynamic 
array of type elem_type)

elem_type[][key_elem_type[]] A;

A is a dynamic array of type (associative array that maps 
a key of type (dynamic array of type key_elem_type) to a value 
type of elem_type)

elem_type [key_elem_type[]][5][6] A;

A is an associative array that maps a key of type (dynamic array 
of type key_elem_type) to a value type of (an array of length 5 of 
type (array of length 6 of type elem_type)))

So can parentheses be used to associate types together?

(char[])[char[]] A; // Same as char[char[]][] map;

This reads a little better to me because it retains the pattern

value_type[key_type] A;
May 26 2002
prev sibling parent reply "Nic Tiger" <nictiger pt.comcor.ru> writes:
Maybe I'm wrong but you should change braces order:
  char[char[]][] map;

Then I think map["..."] will refer to char[] element.

"Patrick Down" <pat codemoon.com> wrote in message
news:Xns9209D80E1BC4Bpatcodemooncom 63.105.9.61...
 What I want is an associative array of char[] to char[]

 The following program generates this compiler error:
 test.d(9): cannot implicitly convert wchar[3] to int
 Line 9 is map["str"] = "str";

 int main(char[][] args)

   char[][char[]] map;

   map["str"] = "str";

   return 0;
 }

 This little work around fixes the problem.

 alias char[] string;

 int main(char[][] args)

   string[char[]] map;

   map["str"] = "str";

   return 0;
 }

May 09 2002
parent Patrick Down <pat codemoon.com> writes:
"Nic Tiger" <nictiger pt.comcor.ru> wrote in news:abfjm4$1m56$1
 digitaldaemon.com:

 Maybe I'm wrong but you should change braces order:
   char[char[]][] map;

Well I could be wrong but I think you read it right to left So what you have is... [] a dynamic array [char[]][] a dynamic array of assoc array with char[] as the key char[char[]][] a dynamic array of assoc array with char[] as the key that maps to a single char Or another way to look ar it is... alias char[char[]] charmap; char[] -> char charmap[] map;
May 09 2002