Associative Array Problem
Steven Schveighoffer
schveiguy at yahoo.com
Mon Mar 24 13:25:57 PDT 2008
"Steven Schveighoffer" wrote
> "Wolftousen Frozenwind" 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 = 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.
Err... 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.
-Steve
More information about the Digitalmars-d-learn
mailing list