Detect empty associated array of an associated array Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
toralf
Posts: 868
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

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: 4709
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

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

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

; ...
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: 868
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: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Detect empty associated array of an associated array

18 Sep 2016, 03:17

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]
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb, mikeyww and 315 guests