Detect empty associated array of an associated array Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
toralf
Posts: 469
Joined: 27 Apr 2014, 21:08
Location: Germany

Detect empty associated array of an associated array  Topic is solved

17 Sep 2016, 02:45

I have an associated array A that holds associated arrays Bs.
How can I detect if one of the B Arrays has become an empty array {}?

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

ciao
toralf
Helgef
Posts: 3096
Joined: 17 Jul 2016, 01:02
Contact:

Re: Detect empty associated array of an associated array

17 Sep 2016, 02:54

MaxIndex() and Length() aren't very useful for non-integer keys. You can count the keys in the sub-arrays, using a nested for-loop:

Code: [Select all] [Download] GeSHi © Codebox Plus

For Key, oB in A {
ctr:=0
For k in oB
ctr++
MsgBox, % Key " lentgth: " ctr
}
kon
Posts: 1760
Joined: 29 Sep 2013, 17:11

Re: Detect empty associated array of an associated array

17 Sep 2016, 08:34

Good idea. Building on that, I would suggest breaking the for-loop after the first iteration because needlessly looping through a large array will hurt performance.

Code: [Select all] [Download] GeSHi © Codebox Plus

; ...
MsgBox, % IsEmpty(A["X"]) ; 0 - not empty
MsgBox, % IsEmpty(A["T"]) ; 1 - empty
return

IsEmpty(Arr) {
Arr._NewEnum()[k, v] ; <-- this is basically a for-loop that only gets the first key/value pair.
return k = ""
}
toralf
Posts: 469
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: Detect empty associated array of an associated array

17 Sep 2016, 13:57

Thanks a lot for the help.
I liked the _NewEnum idea a lot.
ciao
toralf
lexikos
Posts: 6045
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Detect empty associated array of an associated array

18 Sep 2016, 03:17

Code: [Select all] [Download] GeSHi © Codebox Plus

MsgBox % IsEmpty({"": "not empty"}) ; 1

IsEmpty(Arr) {
Arr._NewEnum()[k, v]
return k = ""
}
Fail.

You should use the return value of _NewEnum, not the returned key.

Code: [Select all] [Download] GeSHi © Codebox Plus

MsgBox % IsEmpty({"": "not empty"}) ; 0
MsgBox % IsEmpty({}) ; 1

IsEmpty(Arr) {
return !Arr._NewEnum()[k, v]
}

Return to “Ask For Help”

Who is online

Users browsing this forum: Hellbent, ZeroX4 and 65 guests