- Two types of syntax are used:
[enum.next(k,v)]
For-loop
https://autohotkey.com/docs/commands/For.htm
[enum[k,v]]
Enumerator
https://autohotkey.com/docs/objects/Enumerator.htm
Code: Select all
q:: ;for loop via while
;the Next method returns 1 or 0, and outputs vKey/vValue ByRef
;as separate lines instead of a loop
;oArray := ["a", "b", "c"]
;oEnum := oArray._NewEnum()
;vRet := oEnum.Next(vKey, vValue)
;MsgBox, % "[" vRet "] " vKey " " vValue
;vRet := oEnum.Next(vKey, vValue)
;MsgBox, % "[" vRet "] " vKey " " vValue
;vRet := oEnum.Next(vKey, vValue)
;MsgBox, % "[" vRet "] " vKey " " vValue
;vRet := oEnum.Next(vKey, vValue)
;MsgBox, % "[" vRet "] " vKey " " vValue
;using enum.next(k,v) syntax (see link for an example)
;For-loop
;https://autohotkey.com/docs/commands/For.htm
oArray := ["a", "b", "c"]
oEnum := oArray._NewEnum()
while oEnum.Next(vKey, vValue)
MsgBox, % vKey " " vValue
;the same loop again but using enum[k,v] syntax (see link for an example)
;Enumerator
;https://autohotkey.com/docs/objects/Enumerator.htm
oEnum := oArray._NewEnum()
while oEnum[vKey, vValue]
MsgBox, % vKey " " vValue
;the same loop again but using a for loop
for vKey, vValue in oArray
MsgBox, % vKey " " vValue
;note: returns nothing
oEnum := oArray._NewEnum()
for vKey, vValue in oEnum
MsgBox, % vKey " " vValue
;note: returns nothing
for vKey, vValue in oArray._NewEnum()
MsgBox, % vKey " " vValue
return
w:: ;in this example, we see what happens if we keep applying Next, even after it has returned a 0
oArray := ["a", "b", "c"]
oEnum := oArray._NewEnum()
Loop, 6
{
vRet := oEnum.Next(vKey, vValue)
MsgBox, % "[" vRet "] " vKey " " vValue
}
return
e:: ;in this example we repeatedly restart the loop
oArray := ["a", "b", "c", "d", "e", "f"]
oEnum := oArray._NewEnum()
Loop, 10
{
if (Mod(A_Index, 3) = 1)
oEnum := oArray._NewEnum()
oEnum.Next(vKey, vValue)
MsgBox, % vKey " " vValue
}
return
r:: ;this produces two separate enumerators, so the loops don't interfere with each other and can occur separately
vOutput := ""
oArray := StrSplit("abc")
for vKey, vValue in oArray
{
vOutput .= "1 " vKey " " vValue "`r`n"
for vKey, vValue in oArray
vOutput .= "`t" "2 " vKey " " vValue "`r`n"
}
MsgBox, % vOutput
return
t:: ;what happens if we delete a key partway through using RemoveAt/Delete
vOutput := ""
oArray := StrSplit("abc")
for vKey, vValue in oArray
{
if (oArray.2 = "b")
oArray.RemoveAt(2)
vOutput .= "1 " vKey " " vValue "`r`n"
for vKey, vValue in oArray
vOutput .= "`t" "2 " vKey " " vValue "`r`n"
}
MsgBox, % vOutput
vOutput := ""
oArray := StrSplit("abc")
for vKey, vValue in oArray
{
if (oArray.2 = "b")
oArray.Delete(2)
vOutput .= "1 " vKey " " vValue "`r`n"
for vKey, vValue in oArray
vOutput .= "`t" "2 " vKey " " vValue "`r`n"
}
MsgBox, % vOutput
return
- Do they typically contains keys, methods, properties, what?
Code: Select all
q:: ;what information can we reveal about enum objects
;these attempts reveal nothing
oArray := ["a", "b", "c"]
oEnum := oArray._NewEnum()
vOutput := ""
for vKey, vValue in oEnum
vOutput .= vKey " " vValue "`r`n"
MsgBox, % vOutput
oEnum := oArray._NewEnum()
vOutput := ""
for vKey, vValue in oEnum.base
vOutput .= vKey " " vValue "`r`n"
MsgBox, % vOutput
return
For-loop
https://autohotkey.com/docs/commands/For.htm
Enumerator
https://autohotkey.com/docs/objects/Enumerator.htm
Object
https://autohotkey.com/docs/objects/Object.htm
ComObjArray()
https://autohotkey.com/docs/commands/ComObjArray.htm
ComObjGet()
https://autohotkey.com/docs/commands/ComObjGet.htm