objects: 0-based array, shift key numbers by an offset

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: 0-based array, shift key numbers by an offset

17 Jul 2017, 10:54

I recently wanted to make an array with a 0-based index. (Although generally I prefer a 1-based index.) I wondered if anyone had any other ideas re. this issue. Thanks.

I've also tried here to shift an array to start at key -5 or 6, although to shift it upwards appears to be more difficult, if anyone can better the script below, although I did find a reasonably good solution, if a StrRept function is used.

Code: Select all

q:: ;0-based array, shift key numbers by an offset
Loop, 4
{
	(A_Index = 1) && (oArray := StrSplit("zero,one,two,three,four,five", ",")).RemoveAt(0)
	(A_Index = 2) && (oArray := StrSplit("minus five,minus four,minus three,minus two,minus one,zero", ",")).RemoveAt(-5,6)
	if (A_Index = 3)
	{
		oArray := StrSplit("six,seven,eight,nine,ten", ",")
		Loop, 5
			oArray.InsertAt(1, ""), oArray.Delete(1)
	}
	(A_Index = 4) && ((oArray := StrSplit("six,seven,eight,nine,ten", ",")).InsertAt(1, StrSplit(StrRept(",",5))*), oArray.Delete(1, 5))
	vOutput := ""
	for vKey, vValue in oArray
		vOutput .= vKey " " vValue "`r`n"
	MsgBox, % vOutput
}
oArray := ""
return

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

StrRept(vText, vNum)
{
	return StrReplace(Format("{:" vNum "}","")," ",vText)
	;return StrReplace(Format("{:0" vNum "}",0),0,vText)
}
==================================================

[EDIT:] I just thought of a simpler more general method, although the earlier methods can be useful, if you want to modify an existing array.

Code: Select all

q:: ;0-based array, shift key numbers by an offset
Loop, 3
{
	(A_Index = 1) && (oArray := {}).InsertAt(0, StrSplit("zero,one,two,three,four,five", ",")*)
	(A_Index = 2) && (oArray := {}).InsertAt(-5, StrSplit("minus five,minus four,minus three,minus two,minus one,zero", ",")*)
	(A_Index = 3) && (oArray := {}).InsertAt(6, StrSplit("six,seven,eight,nine,ten", ",")*)
	vOutput := ""
	for vKey, vValue in oArray
		vOutput .= vKey " " vValue "`r`n"
	MsgBox, % vOutput
}
oArray := ""
return
==================================================

[EDIT:] Re. a 1-based index, so maybe I agree with the dog.
« What's on your mind? » - Page 168 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 27#p156027
Last edited by jeeswg on 17 Jul 2017, 13:00, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: objects: 0-based array, shift key numbers by an offset

17 Jul 2017, 12:17

Code: Select all

Arr := []
string := "zero,one,two,three,four,five"

Loop, Parse, string, CSV
{
    if (A_Index = 1)
		Arr[0] := A_LoopField
	else
		Arr.Push(A_LoopField)
}
MsgBox,, 0 based array, % Arr.0, 1
MsgBox,, 0 based array, % Arr.1, 1
MsgBox,, 0 based array, % Arr.2, 1
MsgBox,, 0 based array, % Arr.3, 1
MsgBox,, 0 based array, % Arr.4, 1
MsgBox,, 0 based array, % Arr.5, 1

Code: Select all

Arr := []
string := "zero,one,two,three,four,five"

Loop, Parse, string, CSV
	Arr.InsertAt(A_Index - 1, A_LoopField) 

MsgBox,, 0 based array, % Arr.0, 1
MsgBox,, 0 based array, % Arr.1, 1
MsgBox,, 0 based array, % Arr.2, 1
MsgBox,, 0 based array, % Arr.3, 1
MsgBox,, 0 based array, % Arr.4, 1
MsgBox,, 0 based array, % Arr.5, 1
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: objects: 0-based array, shift key numbers by an offset

17 Jul 2017, 18:55

Hello.
You might be interested in ComObjArray.
To make an i-indexed based array, we can do arr:={(i-1):""}. But that messes up the enumerator. An attempt to make a i-indexed based array with working enumerator:

Code: Select all

iarr(i,byref k:="", byref v:=""){
	; array, i indexed based
	if isObject(i){
		if i.haskey("next"){
			r:=i.o.next(k,v)
			return k="_NewEnum" ? false : r
		}
		return {next:func(A_ThisFunc),o:i}
	}
	p:={(i-1):""},o:=p._NewEnum(),o.next(),p._NewEnum:=Func(A_ThisFunc).Bind(o)
	return p
}
; Example:
i:=0
oArray:=iArr(i)
oArray.push(37)
oArray.push(42)
oArray[555]:=33
msgbox, % oArray[i]
for k, v in oArray
	msgbox % k "`n" v
I guess it is sort of a misnomer to call an ahk array i-indexed based, it is i-indexed based w.r.t. to the enumerator perhaps. :think:

Cheers.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doodles333, mikeyww and 276 guests