How to reverse the order of elements?

Post a reply

Confirmation code
Enter the code exactly as it appears. All letters are case insensitive.
Smilies
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :| :mrgreen: :geek: :ugeek: :arrow: :angel: :clap: :crazy: :eh: :lolno: :problem: :shh: :shifty: :sick: :silent: :think: :thumbup: :thumbdown: :salute: :wave: :wtf: :yawn: :facepalm: :bravo: :dance: :beard: :morebeard: :xmas: :HeHe: :trollface: :cookie: :rainbow: :monkeysee: :monkeysay: :happybday: :headwall: :offtopic: :superhappy: :terms: :beer:
View more smilies

BBCode is ON
[img] is OFF
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: How to reverse the order of elements?

Re: How to reverse the order of elements?

Post by Helgef » 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]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.

Re: How to reverse the order of elements?

Post by magicinmath » 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.

Re: How to reverse the order of elements?

Post by A_AhkUser » 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.

Re: How to reverse the order of elements?

Post by magicinmath » 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?

Re: How to reverse the order of elements?

Post by Helgef » 18 May 2017, 13:16

A_AhkUser wrote:

Code: [Select all]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]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
}

Re: How to reverse the order of elements?

Post by IMEime » 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

Re: How to reverse the order of elements?

Post by jNizM » 18 May 2017, 00:55

Re: How to reverse the order of elements?

Post by IMEime » 17 May 2017, 23:38

Code: [Select all]GeSHi © Codebox Plus

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

Re: How to reverse the order of elements?

Post by A_AhkUser » 17 May 2017, 23:37

Code: [Select all]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]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]

How to reverse the order of elements?

Post by magicinmath » 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]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.

Top