How to find associative array length in AutoHotkey?

If you use the length () function in an associative array , it will return the "largest index" used in the array. So, if you have keys that are not integers, length () will not return the actual number of elements in your array. (And this can happen for other reasons.)

Is there a more useful version of length () for finding the length of an associative array?

Or do I need to actually scroll and count each element? I’m not sure how to do this without knowing in advance all the possible keys.

+4
source share
1 answer

, Array.MaxIndex() . , AutoHotKey , 1, MaxIndex() .

, -, , MaxIndex() null.

.

DesiredDroids := object()
DesiredDroids["C3P0"] := "Gold"
DesiredDroids["R2D2"] := "Blue&White"
count :=0
for key, value in DesiredDroids
    count++
MsgBox, % "We're looking for " . count . " droid" . ( count=1 ? "" : "s" ) . "."

We're looking for 2 droids.
+3

All Articles