Help with Arrays/Objects Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
L40P
Posts: 19
Joined: 05 Jul 2016, 15:49

Help with Arrays/Objects

22 Jul 2018, 07:48

Hi,
for my latest project I need to learn stuff about Arrays/Objects, but there are some things I just don't understand.

1.

Code: Select all

Arr := {Number1: 1, Number2: 2, Number3: 3}
MsgBox % Arr.Count()
Why doesn't this say "3" like it even says in the Doc, but instead nothing?

2. How do I add a new key-value pair or edit the value when the key already exists? Is there a function already doing that or do I have to check using for example if-statements?
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Help with Arrays/Objects

22 Jul 2018, 08:34

Try this:

Code: Select all

Arr := {Number1: 1, Number2: 2, Number3: 3}
MsgBox % Arr.Count() ; 3

Arr["Number4"] := 4
MsgBox % Arr.Count() ; 4

Arr.Number2 := "b"
MsgBox % Arr.Number2 ; b
I hope that helps.
gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: Help with Arrays/Objects  Topic is solved

22 Jul 2018, 08:35

Why doesn't this say "3" like it even says in the Doc, but instead nothing?
Well, it actually says 3... here. But the docs say that you need an AHK version 1.1.29+... so it gotta be quite new. I wasn't even really aware that it was added recently.

By default, any existing key will be overwritten, if you assign something (new) to it. But you can check for existing keys with Haskey():

Code: Select all

Arr := {Number1: 1, Number2: 2, Number3: 3}
Arr.Number1 := 4

for key, val in Arr
	msgbox % key " : " val

if arr.Haskey("Number2")
	msgbox stop. Number2 already exists
else
	Arr.Number2 := 5

if arr.Haskey("Number4")
	msgbox stop. Number 4 already exists
else
	Arr.Number4 := 5

for key, val in Arr
	msgbox % key " : " val
Not existing keys will be created, when you assign a value to a key.
User avatar
L40P
Posts: 19
Joined: 05 Jul 2016, 15:49

Re: Help with Arrays/Objects

22 Jul 2018, 09:18

Thanks! Wasn't aware of the newest updates. They should make a newsletter to let people know.
gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: Help with Arrays/Objects

22 Jul 2018, 11:22

Yeah, perhaps. I look at the change log from time to time... https://autohotkey.com/docs/AHKL_ChangeLog.htm
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: Help with Arrays/Objects

23 Jul 2018, 03:30

New versions are announced in the announcement sub-forum. The current version is shown at the top of every page of this forum, next to "Download" (least if you're using the default forum theme and not on a mobile). You can also write a script to check version.txt periodically...

I am not a fan of sending or receiving "newsletters".

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: CrowexBR, vysmaty and 259 guests