www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - associative array with element type struct ?

reply Paul <phshaffer gmail.com> writes:
Can I have an associative array with the element type being a 
struct?
..this
26  // simple dual tone key struct
27  struct keytones { ushort rowFreq; ushort colFreq; }
28
29  // keypad associative array
30  keytones[string] keypad;
31  keypad["1"].rowFreq = 697; keypad["1"].colFreq = 1209;

...gets this response from DMD compiler

aaa.d(30): Error: undefined identifier `keypad`
aaa.d(30): Error: declaration `aaa.main.keytones` is already 
defined
aaa.d(27):        `struct` `keytones` is defined here

thanks for any assistance!
Sep 23 2021
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 9/23/21 3:06 PM, Paul wrote:
 Can I have an associative array with the element type being a struct?
Of course! And it's very common. :)
 ...this
 26  // simple dual tone key struct
 27  struct keytones { ushort rowFreq; ushort colFreq; }
 28
 29  // keypad associative array
 30  keytones[string] keypad;
 31  keypad["1"].rowFreq = 697; keypad["1"].colFreq = 1209;

 ....gets this response from DMD compiler

 aaa.d(30): Error: undefined identifier `keypad`
I think you got that error from a different experiment because 'keypad' is clearly valid in that code. However, you are attempting to access members of a non-existing element: There is no element corresponding to "1" in the AA (yet). There are multiple ways of populating an AA but I usually and simply assign an rvalue in such cases: void main() { // simple dual tone key struct struct keytones { ushort rowFreq; ushort colFreq; } // keypad associative array keytones[string] keypad; keypad["1"] = keytones(697, 1209); } Ali
Sep 23 2021
parent Paul <phshaffer gmail.com> writes:
 Of course! And it's very common. <= lol
Thanks Ali. Much appreciated!
Sep 23 2021