How to reverse the order of elements? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
magicinmath
Posts: 140
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] [Expand] [Download] GeSHi © Codebox Plus



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: 566
Joined: 06 Mar 2017, 16:18
Location: France

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

17 May 2017, 23:37

Code: [Select all] [Download] GeSHi © Codebox Plus

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

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


Code: [Select all] [Download] GeSHi © Codebox Plus

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]
Video Game Reader
Image
github
demo
IMEime
Posts: 411
Joined: 20 Sep 2014, 06:15

Re: How to reverse the order of elements?

17 May 2017, 23:38

Code: [Select all] [Download] GeSHi © Codebox Plus

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

Re: How to reverse the order of elements?

18 May 2017, 00:55

[AHK] 1.1.26.01 x64 Unicode | [WIN] 10 Pro (Version 1703) x64
My GitHub Profile | Donations are appreciated if I could help you
IMEime
Posts: 411
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/custom-functions/sortarray
Helgef
Posts: 2420
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] [Download] GeSHi © Codebox Plus

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] [Download] GeSHi © Codebox Plus

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: 140
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: 566
Joined: 06 Mar 2017, 16:18
Location: France

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.
Video Game Reader
Image
github
demo
magicinmath
Posts: 140
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: 2420
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] [Download] GeSHi © Codebox Plus

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.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], ravena1 and 46 guests