Page 1 of 1

How to reverse the order of elements?

Posted: 17 May 2017, 22:51
by magicinmath
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.

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

Posted: 17 May 2017, 23:37
by A_AhkUser

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]

Re: How to reverse the order of elements?

Posted: 17 May 2017, 23:38
by IMEime

Code: Select all

myVar := "12345"
Loop, Parse, % myVar
myResult := A_LoopField myResult
MsgBox % myResult

Re: How to reverse the order of elements?

Posted: 18 May 2017, 00:55
by jNizM

Re: How to reverse the order of elements?

Posted: 18 May 2017, 03:30
by IMEime
jNizM's post is good.

I found another one too.

AHK References
https://sites.google.com/site/ahkref/cu ... /sortarray

Re: How to reverse the order of elements?

Posted: 18 May 2017, 13:16
by Helgef
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
}

Re: How to reverse the order of elements?

Posted: 19 May 2017, 18:21
by magicinmath
Thanks everyone, I appreciate all the different versions.

Is it possible someone could point out the error in my code specifically?

Re: How to reverse the order of elements?

Posted: 19 May 2017, 18:45
by A_AhkUser
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.

Re: How to reverse the order of elements?

Posted: 19 May 2017, 19:10
by magicinmath
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.

Re: How to reverse the order of elements?

Posted: 19 May 2017, 23:12
by Helgef
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.

Re: How to reverse the order of elements?

Posted: 25 Sep 2019, 04:39
by mdimovic

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%