Reverse array Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
masheen
Posts: 295
Joined: 06 Dec 2016, 14:10

Reverse array

04 Nov 2017, 04:54

How to reverse array that get:

Code: Select all

oArray := []
oArray[1] := 1
oArray[7] := 2
oArray[8] := 3

Msgbox, % oArray[1] " " oArray[7] " " oArray[8] ; 1 2 3

ReverseArray(oArray)

Msgbox, % oArray[1] " " oArray[7] " " oArray[8] ; 3 2 1


ReverseArray(oArray){
	.... ;???
	.... ;???
	.... ;???
}
User avatar
KernelKross
Posts: 12
Joined: 01 Nov 2017, 09:16

Re: Reverse array

04 Nov 2017, 05:01

Code: Select all

ReverseArray(oArray)
{
	Array := Object()
	For i,v in oArray
		Array[oArray.Length()-i+1] := v
	Return Array
}
Or use ByRef to change the passed variable upon return.

Code: Select all

ReverseArray(ByRef oArray)
{
	Array := Object()
	For i,v in oArray
		Array[oArray.Length()-i+1] := v
	Return Array
}
User avatar
masheen
Posts: 295
Joined: 06 Dec 2016, 14:10

Re: Reverse array

04 Nov 2017, 05:09

KernelKross wrote:

Code: Select all

ReverseArray(oArray)
{
	Array := Object()
	For i,v in oArray
		Array[oArray.Length()-i+1] := v
	Return Array
}
Or use ByRef to change the passed variable upon return.

Code: Select all

ReverseArray(ByRef oArray)
{
	Array := Object()
	For i,v in oArray
		Array[oArray.Length()-i+1] := v
	Return Array
}

Dont work

Code: Select all

oArray := []
oArray[1] := 1
oArray[7] := 2
oArray[8] := 3


oArray := ReverseArray(oArray)

Msgbox, % oArray[1] " " oArray[7] " " oArray[8] ; 3 1 <--------------- ???


ReverseArray(oArray)
{
	Array := Object()
	For i,v in oArray
		Array[oArray.Length()-i+1] := v
	Return Array
}
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Reverse array  Topic is solved

04 Nov 2017, 05:18

Code: Select all

q:: ;reverse array
oArray := {}
oArray[1] := 1
oArray[7] := 2
oArray[8] := 3

Msgbox, % oArray[1] " " oArray[7] " " oArray[8] ; 1 2 3

ReverseArray(oArray)

Msgbox, % oArray[1] " " oArray[7] " " oArray[8] ; 3 2 1
return

ReverseArray(oArray)
{
	oArray2 := oArray.Clone()
	oTemp := {}
	for vKey in oArray
		oTemp.Push(vKey)
	vIndex := oTemp.Length()
	for vKey in oArray
		oArray[vKey] := oArray2[oTemp[vIndex--]]
	oArray2 := oTemp := ""
}
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: Reverse array

04 Nov 2017, 07:48

I think you could simplify it jeeswg,

Code: Select all

; Not tested
rev(arr){
	local k, o:=[]
	for k in arr.clone()
		o[k] := arr.pop()
	return o
}
Edit 1 a): Avoids cloning,

Code: Select all

revB(arr){
	local t,k,v,o
	t:=[],o:=[]
	for k in arr
		t[A_Index] := k
	for k, v in t
		o[v] := arr.pop()
	return o
}
Edit 1 b): You do not need to clear local variables before the function returns.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Reverse array

04 Nov 2017, 10:38

@Helgef:
- The nature of masheen's original function request required editing the object 'ByRef', rather than getting a new array returned.
- Does your function handle objects with non-numeric key names? Pop only works on numeric key names AFAIK. [EDIT:] Hmm, the documentation didn't explicitly state this.
- Yes, I'm 50/50 about whether I should clear local variables at the end, for style reasons, and for the situation where you copy out code from a function for use in the main body.of the script.

Code: Select all

q:: ;pop
vOutput := ""
oArray := {}
oArray.a := "A"
oArray.b := "B"
oArray.c := "C"
oArray.Pop()
vOutput := ""
for vKey, vValue in oArray
	vOutput .= vKey " " vValue "`r`n"
MsgBox, % vOutput

vOutput := ""
oArray := ["one","two","three"]
oArray.a := "A"
oArray.b := "B"
oArray.c := "C"
oArray.Pop()
vOutput := ""
for vKey, vValue in oArray
	vOutput .= vKey " " vValue "`r`n"
MsgBox, % vOutput
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: Reverse array

05 Nov 2017, 04:56

- Sure, one can add byref and assign arr:=o instead of returning o.
- No. If this extraordinary case is desirable, your function is fine, but you should not use push when you cannot set the capacity beforehand. (Although, I find it unlikely someone would reverse large (sparse) arrays, so it would probably never matter)
Hmm, the documentation didn't explicitly state this.
Explicitly implict:
pop wrote:it is equivalent to the following:
Value := Object.RemoveAt(Object.Length())
- If it is your style, go ahead ;)
for the situation where you copy out code from a function for use in the main body.of the script.
Blasphemy :D
gmoises
Posts: 74
Joined: 18 Nov 2017, 16:43

Re: Reverse array

17 Oct 2019, 12:05

Sometimes it is important to use 'Local'

Code: Select all

Reverse(Arr) {
	Local
	Rev := Object()
	For Ix, Val in Arr
		Rev.InsertAt(1, Val)
	Return Rev
}
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Reverse array

18 Oct 2019, 01:14

[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
Chunjee
Posts: 1421
Joined: 18 Apr 2014, 19:05
Contact:

Re: Reverse array

20 Oct 2019, 05:27

.reverse is also a method in the biga.ahk library I've been working on (.reverse docs)

Code: Select all

A := new biga()

A.reverse(["a", "b", "c"])
; => ["c", "b", "a"]

A.reverse([{"foo": "bar"}, "b", "c"])
; => ["c", "b", {"foo": "bar"}]

A.reverse([[1, 2, 3], "b", "c"])
; => ["c", "b", [1, 2, 3]]
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Reverse array

20 Oct 2019, 07:30

huh, i thought clones were more expensive than that.
so far it looks like @Helgef's first solution is the simplest, most performant and also conforming to spec
@jeeswg's comes second, while also being correct
and the rest are either unoptimized(insertat 1 big no no) or mangle the indices

in fact, if u clone twice in helgef's function, u get a pretty nice, cheap, correct, immutable array reversal

Code: Select all

rev(arr){
	local k, o:=[]
	brr := arr.clone()
	for k in arr.clone()
		o[k] := brr.pop()
	return o
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: jaka1, mikeyww, ReyAHK, Rohwedder and 297 guests