Understanding Associative arrays

Get help with using AutoHotkey and its commands and hotkeys
User avatar
eblanc
Posts: 70
Joined: 08 May 2019, 14:41

Understanding Associative arrays

12 Dec 2019, 12:45

I'm building a simple mailing list that I want to build using arrays. I've pointed to associative arrays. I've been reading about arrays for a week now. Still don't understand how they work exactly or how you manipulate them. I've looked for tutorials online but they seem pretty vague or convoluted. Please help. Mostly need the theory I think.

So if I want to make a simple list. say.

Name Email
brady [email protected]
jonny [email protected]
Lord Ragnarok [email protected]

I'm guessing I would put it as this:

Code: Select all

Contacts := {brady {"email": "[email protected]"}, (jonny {"email": "[email protected]"}, (Lord Ragnarok {"email": "[email protected]"}}
Correct? just want to make sure syntax is correct also.

so here is my bogus code and as far as I understand.

Code: Select all

array := jonny
Contacts := {brady {"email": "[email protected]"}, (jonny {"email": "[email protected]"}, (Lord Ragnarok {"email": "[email protected]"}}
msgbox, contacts[%array%]??????????
return
My question is, how do I recall information?? I have a feeling that Msgbox is wrong, I also want to make sure I'm using the variables correctly and syntax is correct. I'm not really sure why but I've been really having trouble understanding the logic behind arrays. it seems like a 3rd-dimensional information table that I am having trouble picturing in my brain.

Thanks for the help.
Tom Harding
Posts: 20
Joined: 11 Dec 2019, 13:43

Re: Understanding Associative arrays

12 Dec 2019, 12:56

Code: Select all

Contacts := {"brady":{"email":"[email protected]"},"sandy":{"email":"[email protected]"}}
MsgBox % Contacts["sandy"].email
There are a few ways to go about this, but this is the quick answer.
User avatar
Hellbent
Posts: 925
Joined: 23 Sep 2017, 13:34

Re: Understanding Associative arrays

12 Dec 2019, 13:06

Hope this helps.

Code: Select all


#SingleInstance, Force
#NoEnv
SetBatchLines, -1

Contacts := {Brady: {Email: "[email protected]" , Phone: "123-456-7890"} , Tom: {Email: "" , Phone: ""} , "Mary Ann": {Email: "" , Phone: ""}}

Msgbox,% Contacts.Brady.Email
;or
Msgbox,% Contacts["Brady"].Email
;or
Msgbox,% Contacts["Brady"]["Email"]
;or
Msgbox,% Contacts["Brady","Email"]


;It can sometimes be helpful to break things up a bit.

NewContacts := {}

NewContacts.Sam := {Email: "[email protected]", Phone: "098-765-4321" , "Some key that has spaces in the name": 1234 }
;or
NewContacts.Sam := { Email: "[email protected]"
				   , Phone: "098-765-4321" 
				   , "Some key that has spaces in the name": 1234 }


return
GuiClose:
GuiContextMenu:
*ESC::
	ExitApp
hd0202
Posts: 149
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: Understanding Associative arrays

13 Dec 2019, 08:27

Code: Select all

; msgbox, contacts[%array%]??????????
msgbox, % contacts[array]
Hubert

Return to “Ask For Help”

Who is online

Users browsing this forum: AHK_user, Bing [Bot], Scryptic, sdkyron, submeg and 242 guests