## Detect empty associated array of an associated array Topic is solved

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

### Detect empty associated array of an associated array  Topic is solved

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

``````B1 := {K1: "V1", K2: "V2"}
B2 := {K3: "V3", K4: "V4"}
B3 := {K5: "V5", K6: "V6"}
A  := {X: B1, T: B2, C: B3}
A["T"].Delete("K4")
A["T"].Delete("K3")

For Key, oB in A {
MsgBox % Key                   ;all the values are identical for the different Keys/oBs.
. "`n>"  oB.GetCapacity()
. "<`n>" A[Key].GetCapacity()
. "<`n>" A.GetCapacity()
. "<`n>" isObject(oB)
. "<`n>" oB.MaxIndex()
. "<`n>" A[Key].MaxIndex()
. "<`n>" oB.Length()
. "<`n>" A[Key].Length()
. "<`n`"
}
``````
ciao
toralf
Helgef
Posts: 3198
Joined: 17 Jul 2016, 01:02
Contact:

### Re: Detect empty associated array of an associated array

`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

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

### Re: Detect empty associated array of an associated array

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

``````; ...
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: 506
Joined: 27 Apr 2014, 21:08
Location: Germany

### Re: Detect empty associated array of an associated array

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

### Re: Detect empty associated array of an associated array

Code: Select all

``````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

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

IsEmpty(Arr) {
return !Arr._NewEnum()[k, v]
}``````
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Thanks!

### Who is online

Users browsing this forum: aifritz, boulmers, Google [Bot], TXShooter and 25 guests