How to reverse the order of elements? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
magicinmath
Posts: 162
Joined: 12 Apr 2017, 23:03

How to reverse the order of elements?

17 May 2017, 22:51

Was trying to get this to work, forgive me if theres a command or existing function I can use, and please point it out for me.

Otherwise, what was I doing wrong? I'm missing it...

Code: Select all

	Name:=["1","2","3","4","5"]
	object:=[]
	object:=Name
	n:=0
	
	while(n<NumGet(&object + 4*A_PtrSize)){

		s:=NumGet(&object + 4*A_PtrSize)-n
		++n
		Name[n]:=object[s]
	}
By the end of the loop: Name:= ["5", "4", "3", "4", "5"] Rather than: Name:= ["5", "4", "3", "2", "1"]

Thanks for your time.
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: How to reverse the order of elements?  Topic is solved

17 May 2017, 23:37

Code: Select all

Name:=["1","2","3","4","5"]
	object:=[]
	object:=Name

	object[2] := 9 ; set object
	MsgBox % name[2] ; get name... 9
	

Code: Select all

Name:=["1","2","3","4","5"]


Loop % (_Name:=Name.Clone()).length()
	Name[a_index] := _Name[_Name.length()-a_index+1]

Loop % Name.length()
	MsgBox % Name[a_index]
my scripts
IMEime
Posts: 750
Joined: 20 Sep 2014, 06:15

Re: How to reverse the order of elements?

17 May 2017, 23:38

Code: Select all

myVar := "12345"
Loop, Parse, % myVar
myResult := A_LoopField myResult
MsgBox % myResult
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: How to reverse the order of elements?

18 May 2017, 00:55

[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
IMEime
Posts: 750
Joined: 20 Sep 2014, 06:15

Re: How to reverse the order of elements?

18 May 2017, 03:30

jNizM's post is good.

I found another one too.

AHK References
https://sites.google.com/site/ahkref/cu ... /sortarray
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: How to reverse the order of elements?

18 May 2017, 13:16

A_AhkUser wrote:

Code: Select all

Name:=["1","2","3","4","5"]


Loop % (_Name:=Name.Clone()).length()
	Name[a_index] := _Name[_Name.length()-a_index+1]

Loop % Name.length()
	MsgBox % Name[a_index]
Best suggestion for linear array imo. I'd save the length though, eg

Code: Select all

rev(arr){
	l:=arr.length(),narr:=arr.clone()
	for k, v in arr
		narr[l-k+1]:=v
	return narr ; alt, arr:=narr
}
magicinmath
Posts: 162
Joined: 12 Apr 2017, 23:03

Re: How to reverse the order of elements?

19 May 2017, 18:21

Thanks everyone, I appreciate all the different versions.

Is it possible someone could point out the error in my code specifically?
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: How to reverse the order of elements?

19 May 2017, 18:45

See my answer above, the problem lies on the fact that both object and name dispIte one can believes refers to the same object hence the result: 5 4 3 4 5.


[EDIT]:
Helgef wrote:Best suggestion for linear array imo. I'd save the length though, eg
You're right. Btw before I gave this answer I took again a glance at your answer in another thread, focusing on how initialize static arrays to zero and where you took advantage of VarSetCapacity.

Here's the reason why I think too it is better using the Clone method: Clone can be compare to VarSetCapacity: multiple resizings can be prevented if you have some idea of what the array's' final length will be while, on the contrary - and especially for huge arrays - create the array by means of multiple calls of insertAt seems comparatively less efficient - not sure if I am reasoning right, though.
Last edited by A_AhkUser on 19 May 2017, 19:27, edited 1 time in total.
my scripts
magicinmath
Posts: 162
Joined: 12 Apr 2017, 23:03

Re: How to reverse the order of elements?

19 May 2017, 19:10

A_AhkUser wrote:See my answer above, the problem lies on the fact that both object and name dispate one can believes refers to the same object hence the result: 5 4 3 4 5.
Thanks, I've been meaning to learn how to use loop and for but while is more intuitive to my simpleton mind.

In going over your code it explains a lot about loop I didn't understand :)

Double Thanks.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: How to reverse the order of elements?

19 May 2017, 23:12

You can use setcapacity to avoid resizings. Clone avoids sorting the keys, I assume. Insertat(1) will shift all key-value pairs already in the array, on every call.
Edit:
With shifting paris I mean figurative, {1:"a"}.insertat(1,"b") -> {1:"b", 2:"a"} .... I don't think the values are moved in memory, I would guess no.
Also, an alternative, I think it will perform similar to the clone variants,

Code: Select all

rev(arr){
	sa:=(l:=arr.length())//2	; Edit
	for k, v in arr
		arr[k]:=arr[l-k+1],arr[l-k+1]:=v
	until k==sa
	return
} ; Briefly tested.
If the content of arr are very big strings, we avoid having duplicates of all of them at the same time.

Edit: Fixed weird mistake. It should work now.
mdimovic
Posts: 3
Joined: 27 Feb 2018, 06:48

Re: How to reverse the order of elements?

25 Sep 2019, 04:39

Code: Select all

Myvar:="111`n222`n333"

oArray := StrSplit(Myvar, "`n")


;~ // convert array to string
For Index, Value In oArray
   Str .= "`n" . Value
Str := LTrim(Str, "`n")
MsgBox, %Str%








;~ reverse array order
Loop % (_oArray:=oArray.Clone()).length()
	oArray[a_index] := _oArray[_oArray.length()-a_index+1]
;~ acctualy here
Loop % oArray.length()
	MsgBox % oArray[a_index]








;~ // convert array to string
For Index, Value In oArray
   Str1 .= "`n" . Value
Str1 := LTrim(Str1, "`n")
MsgBox, %Str1%

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 146 guests