digitalmars.D.learn - Associative Array Problem
- Wolftousen Frozenwind (14/14) Mar 24 2008 I'm trying to have an associative array with the key value of type ushor...
- Simen Kjaeraas (13/28) Mar 24 2008 My guess would be that data[keys[x]] does not yet exist when you attempt...
- Steven Schveighoffer (17/31) Mar 24 2008 two problems in your for loop. One is:
- Steven Schveighoffer (5/31) Mar 24 2008 Err... I might be wrong on this :) If some_number is a value and not a
I'm trying to have an associative array with the key value of type ushort. \ struct struct_name { int variable_name; } ushort[some_number] keys; //fill keys //declare my associative array struct_name[ushort] data; //in a loop, filling data for(x = 0; x < keys.length; x++) data[keys[x]].variable_name = some_int; I get an array out of bounds error when running this. Any one help?
Mar 24 2008
On Mon, 24 Mar 2008 20:29:23 +0100, Wolftousen Frozenwind = <eliot.darkwolf gmail.com> wrote:I'm trying to have an associative array with the key value of type =ushort. \ struct struct_name { int variable_name; } ushort[some_number] keys; //fill keys //declare my associative array struct_name[ushort] data; //in a loop, filling data for(x =3D 0; x < keys.length; x++) data[keys[x]].variable_name =3D some_int; I get an array out of bounds error when running this. Any one help?My guess would be that data[keys[x]] does not yet exist when you attempt= = to set its member variable_name. If I am correct, this should work instead: for(x =3D 0; x < keys.length; x++) { data[keys[x]] =3D struct_name(); // call static opCall or other in= it = function here data[keys[x]].variable_name =3D some_int; }
Mar 24 2008
"Wolftousen Frozenwind" wroteI'm trying to have an associative array with the key value of type ushort. \ struct struct_name { int variable_name; } ushort[some_number] keys; //fill keys //declare my associative array struct_name[ushort] data; //in a loop, filling data for(x = 0; x < keys.length; x++) data[keys[x]].variable_name = some_int; I get an array out of bounds error when running this. Any one help?two problems in your for loop. One is: for(x = 0; x < keys.length; x++) Since keys is an associative array, the keys are not guaranteed to be sequential. You should do: foreach(x; keys) // compiler implies that x is the value type of your AA. This also is more efficient than doing the key lookup for each loop. The second problem is your data statement. Just accessing an array element does not create it (this is unlike C++'s std::map). You must create it with an assign statement: foreach(x; keys) { struct_name sn; sn.variable_name = some_int; data[x] = sn; // this creates the element in the array. } -Steve
Mar 24 2008
"Steven Schveighoffer" wrote"Wolftousen Frozenwind" wroteErr... I might be wrong on this :) If some_number is a value and not a type, then you have declared a static array. But I do believe that foreach would make an easier-to-read version of your code at least. -SteveI'm trying to have an associative array with the key value of type ushort. \ struct struct_name { int variable_name; } ushort[some_number] keys; //fill keys //declare my associative array struct_name[ushort] data; //in a loop, filling data for(x = 0; x < keys.length; x++) data[keys[x]].variable_name = some_int; I get an array out of bounds error when running this. Any one help?two problems in your for loop. One is: for(x = 0; x < keys.length; x++) Since keys is an associative array, the keys are not guaranteed to be sequential. You should do: foreach(x; keys) // compiler implies that x is the value type of your AA.
Mar 24 2008