www.digitalmars.com         C & C++   DMDScript  

D - Multidimensional Assiciative Array

reply Darcy Currey <Darcy_member pathlink.com> writes:
Apologies if this has been answered, but I couldn't find a reference.

I am trying to define an associative array, keyed on int[], of int[].
I have tried:
int[int[]][] x;

which compiles OK.

Attempting to use x causes errors.
int[4] y;
if(!y in x) {
x[y].length = 0; // initialise x[y]
}

What are your thoughts?

Darcy Currey
Sep 08 2003
next sibling parent Mike Wynn <mike l8night.co.uk> writes:
Darcy Currey wrote:
 Apologies if this has been answered, but I couldn't find a reference.
 
 I am trying to define an associative array, keyed on int[], of int[].
 I have tried:
 int[int[]][] x;
x is an array of (assoc array which maps int[] to int) this is not what you wanted. D arrays declare are reversed so that typedef foo[B] elem; elem[A] x; is the same as foo[B][A] x unlike C where is would be elem x[A] => foo x[A][B]
 
 which compiles OK.
 
 Attempting to use x causes errors.
 int[4] y;
 if(!y in x) {
 x[y].length = 0; // initialise x[y]
 }
 
 What are your thoughts?
try `int[][int[]] x;` x is assoc array mapping int[] to int[]
 
 Darcy Currey
Sep 08 2003
prev sibling parent Helmut Leitner <helmut.leitner chello.at> writes:
Darcy Currey wrote:
 
 Apologies if this has been answered, but I couldn't find a reference.
 
 I am trying to define an associative array, keyed on int[], of int[].
 I have tried:
 int[int[]][] x;
 
 which compiles OK.
 
 Attempting to use x causes errors.
 int[4] y;
 if(!y in x) {
 x[y].length = 0; // initialise x[y]
 }
 
 What are your thoughts?
 
 Darcy Currey
Perhaps this way: ======================== int [][int[]] x; int [4] y= [1,11,21,31]; int [4] z= [21,20,19,18]; int main() { x[y]=z; x[z]=y; int [][] keys=x.keys; int [] key; int [] val; for(int i=0; i<keys.length; i++) { key=keys[i]; val=x[key]; printf("%d: key: ",i); for(int j=0; j<key.length; j++) printf("%d ",key[j]); printf("val: "); for(int j=0; j<val.length; j++) printf("%d ",val[j]); printf("\n",i); } return 0; } ========================= Output: 0: key: 1 11 21 31 val: 21 20 19 18 1: key: 21 20 19 18 val: 1 11 21 31 -- Helmut Leitner leitner hls.via.at Graz, Austria www.hls-software.com
Sep 09 2003