In AHK if you run a For loop on some array and during that loop delete an item - all the next items' indexes in that array get decremented.
If during the For loop's work you delete an item - your loop from now on will not iterate through all the next items after the deleted one.
Code: Select all
Arr := ["a", "b", "c", "d", "e"]
For k, v In Arr
{
If (A_Index < 4) ; Delete first 3 items.
Arr.Remove(A_Index)
Else ; This will never get executed, although the array still contains some items, that were not iterated through by the loop.
MsgBox, %v% ; This should have worked for the 4th and the 5th iterations of the loop, but because the items in the array got shifted - the loop stopped after just 3 iterations.
}
Example task:
You have an array Arr := [1, 0, 6, 2, 0, 2, 9, 1, 1, 1, 54] in which you need to delete all the items, who's value is bigger than 1.
This will fail now (but similar thing would work totally fine in Python):
Code: Select all
Arr := [1, 0, 6, 2, 0, 2, 9, 1, 1, 1, 54]
For k, v In Arr
(v > 1 ? Arr.Remove(k) : "")
Code: Select all
Arr := [1, 0, 6, 2, 0, 2, 9, 1, 1, 1, 54], RemoveTheseIndexes := ""
For k, v In Arr
(v > 1 ? RemoveTheseIndexes .= "|" k : "")
RemoveTheseIndexes := LTrim(RemoveTheseIndexes, "|")
Sort, RemoveTheseIndexes, N R D|
Loop, Parse, RemoveTheseIndexes, |
Arr.Remove(A_LoopField)
Removetheseindexes := ""