objects: loop and delete keys

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

objects: loop and delete keys

08 Jul 2017, 16:44

I was doing some experiments on looping through objects and deleting objects at the same time. For a for loop, perhaps the only realistic option, is to create a new array of keys that you want to keep rather than to try and delete keys on the fly.

So I'm just posting this in case anyone has anything interesting to say on the topic, or has some good links re. this issue. Cheers.

Code: Select all

;q:: ;objects - loop and delete - delete all (undesirable results)
oArray := StrSplit("abcdefghijklmnopqrstuvwxyz")
vOutput := ""
Loop, % oArray.Length()
{
	vOutput .= oArray[A_Index]
	oArray.RemoveAt(A_Index)
}
MsgBox, % vOutput " " StrLen(vOutput) ;acegikmoqsuwy 13
MsgBox, % oArray.Length()
oArray := ""
return

;w:: ;objects - loop and delete - delete all
oArray := StrSplit("abcdefghijklmnopqrstuvwxyz")
vOutput := ""
Loop, % oArray.Length()
{
	vOutput .= oArray[1]
	oArray.RemoveAt(1)
}
MsgBox, % vOutput " " StrLen(vOutput) ;abcdefghijklmnopqrstuvwxyz 26
MsgBox, % oArray.Length()
oArray := ""
return

;==================================================

;q:: ;objects - loop and delete - forwards loop
Loop, 2
{
	vKeepVowels := (A_Index = 1)
	oArray := StrSplit("abcdefghijklmnopqrstuvwxyz")
	vIndex := 0
	Loop
	{
		vIndex++
		if !oArray.HasKey(vIndex)
			break
		vValue := oArray[vIndex]
		if vKeepVowels && !RegExMatch(vValue, "[aeiou]")
			oArray.RemoveAt(vIndex), vIndex -= 1
		if !vKeepVowels && RegExMatch(vValue, "[aeiou]")
			oArray.RemoveAt(vIndex), vIndex -= 1
	}
	vOutput := ""
	Loop, % oArray.Length()
		vOutput .= oArray[A_Index]
	MsgBox, % vOutput " " StrLen(vOutput)
}
return

;==================================================

;q:: ;objects - loop and delete - backwards loop
Loop, 2
{
	vKeepVowels := (A_Index = 1)
	oArray := StrSplit("abcdefghijklmnopqrstuvwxyz")
	Loop, % vCount := oArray.Length()
	{
		vIndex := vCount+1-A_Index
		if !oArray.HasKey(vIndex)
			break
		vValue := oArray[vIndex]
		if vKeepVowels && !RegExMatch(vValue, "[aeiou]")
			oArray.RemoveAt(vIndex)
		if !vKeepVowels && RegExMatch(vValue, "[aeiou]")
			oArray.RemoveAt(vIndex)
	}
	vOutput := ""
	Loop, % oArray.Length()
		vOutput .= oArray[A_Index]
	MsgBox, % vOutput " " StrLen(vOutput)
}
oArray := ""
return

;==================================================

;q:: ;objects - loop and delete - for loop (undesirable results)
oArray := StrSplit("abcdefghijklmnopqrstuvwxyz")
for vKey, vValue in oArray
	oArray.RemoveAt(vKey)
vOutput := ""
for vKey, vValue in oArray
	vOutput .= oArray[A_Index]
MsgBox, % vOutput " " StrLen(vOutput) ;bdfhjlnprtvxz 13
return

;w:: ;objects - loop and delete - for loop (undesirable results)
oArray := StrSplit("abcdefghijklmnopqrstuvwxyz")
for vKey, vValue in oArray
	oArray.Delete(vKey)
vOutput := ""
for vKey, vValue in oArray
	vOutput .= oArray[A_Index]
MsgBox, % vOutput " " StrLen(vOutput) ;bdfhjl 6
return

;==================================================

;q:: ;objects - loop and push to new array - for loop
Loop, 2
{
	vKeepVowels := (A_Index = 1)
	oArray := StrSplit("abcdefghijklmnopqrstuvwxyz")
	oArray2 := []
	for vKey, vValue in oArray
	{
		if vKeepVowels && RegExMatch(vValue, "[aeiou]")
			oArray2.Push(vValue)
		if !vKeepVowels && !RegExMatch(vValue, "[aeiou]")
			oArray2.Push(vValue)
	}
	vOutput := ""
	Loop, % oArray2.Length()
		vOutput .= oArray2[A_Index]
	MsgBox, % vOutput " " StrLen(vOutput)
}
oArray := oArray2 := ""
return

;==================================================

;q:: ;objects - potentially infinite loop - for loop
oArray := ["a"]
for vKey, vValue in oArray
{
	if (A_Index = 1000)
		break
	oArray.Push("x")
}
MsgBox, % oArray.Length()
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: objects: loop and delete keys

08 Jul 2017, 16:55

Hello jeeswg.
I'd say most often you do not want to delete while for-looping. But ofc, it depends on what you are doing. Also, you might find pop useful

Code: Select all

oArray := StrSplit("nrocpop")
vOutput := ""
Loop, % oArray.Length()
	vOutput .= oArray.Pop()
	; vOutput := oArray.Pop() . vOutput ; Alt.
	
MsgBox, % vOutput " " StrLen(vOutput) ;acegikmoqsuwy 13
MsgBox, % oArray.Length()
oArray := ""
return
Chees :wave:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: FanaticGuru, filipemb and 289 guests