objects: popping empty 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: popping empty keys

17 Dec 2017, 15:35

- I've been working on, you guessed it, a converter.
- Popping empty keys is useful, to remove any trailing blank parameters, after rearranging parameters.
- Why would you rearrange parameters? For certain functions in AHK v2, the parameter order has changed.
- I'm sharing this in case anyone has interesting to add on the topic.

Code: Select all

q:: ;pop empty keys
oArray := StrSplit("a,b,c,,,", ",")
MsgBox, % oArray.Length()
JEE_ObjPopBlank(oArray)
MsgBox, % oArray.Length()

oArray := StrSplit("a,b,c,,,", ",")
oArray.10 := ""
MsgBox, % oArray.Length()
JEE_ObjPopBlank(oArray)
MsgBox, % oArray.Length()

oArray := ""
return

JEE_ObjPopBlank(oArray)
{
	Loop, % oArray.Length()
	{
		if (oArray[oArray.Length()] = "")
			oArray.Pop()
		else
			break
	}
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: objects: popping empty keys

20 Dec 2017, 16:37

Hi jeeswg,

Unless I'm mistaken, you can avoid using multiple times the length method upon the reference passed to the caller, decreasing a variable instead and also benefiting from the short-circuit boolean evaluation - I'm not sure if it is more efficient though:

Code: Select all

JEE_ObjPopBlank(oArray) {
	Loop % (l:=oArray.length()) {
	; i := a_index
	if ((oArray[l--] == "") && (e:=oArray.pop()) && oArray.push(e) || (oArray[l] <> ""))
			break
	}
	; MsgBox, % i
}
my scripts
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: objects: popping empty keys

20 Dec 2017, 18:04

- Thanks A_AhkUser. I had thought about pop until it's not blank and then push, it's interesting to see pop and push there in the same line. One reason I'd done my function like I had, was in case there were big gaps between keys, and therefore to not check for keys one by one. Realistically though, whenever I would use it, performance would never be an issue. (Less than 20 keys.)
- With pop and then push to undo, you have to be careful, e.g.:

Code: Select all

q:: ;pop, and push to undo, doesn't work, key 10 becomes key 2
oArray := {}
oArray.1 := "a"
oArray.10 := "j"
oArray.Push(oArray.Pop())
MsgBox, % oArray.Length()
MsgBox, % oArray[2]
return

Code: Select all

q:: ;pop and preview
oArray := {}
oArray.1 := "a"
oArray.10 := "j"
oArray.20 := ""
JEE_ObjPopBlank(oArray)
MsgBox, % JEE_ObjPreview(oArray)
return

JEE_ObjPreview(oArray)
{
	vOutput := ""
	for vKey, vValue in oArray
		vOutput .= vKey " " vValue "`r`n"
	return SubStr(vOutput, 1, -2)
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: objects: popping empty keys

21 Dec 2017, 08:47

With pop and then push to undo, you have to be careful
Thanks for the precision and your examples: actually, I wasn't aware of this behaviour...

Cheers (and, by the way, happy holidays :xmas:)
my scripts
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: objects: popping empty keys

21 Dec 2017, 20:39

Cheers. It's amusing all the nuances of AHK objects and objects generally. I see you and a few other users getting into the details of AHK object classes from time to time. Btw I like the picture (Video Game Reader) in your signature, very sleek design, nicely arranged and coloured. Merry Christmas/Happy Holidays :xmas:.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], RandomBoy, Rohwedder and 398 guests