Sort variables Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
mast4rwang
Posts: 65
Joined: 19 Jul 2017, 09:59

Sort variables

23 Sep 2017, 15:35

Hello, guys. I spent hours trying to solve this puzzle to no avail :shock:

My goal is to sort the variables using the second column numbers in ascending order.

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



As you can see, id2 is the smallest with 200 so its counterpart v2 goes to c1, the first on the list and so on. How to sort this? :shock:
User avatar
TLM
Posts: 1146
Joined: 01 Oct 2013, 07:52

Re: Sort variables

23 Sep 2017, 16:15

there are several ways you can approach this, i would use arrays..
this is just a demonstration off the top of my head
( I'm not a fan of pseudo arrays! eg id%a_index%, c%a_index%, ... )

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

In the final you'd want to remove pseudo arrays and just use real arrays ( if possible ).

edit: actually in this case pseudo arrays are ok if you're assigning variables to be used at a later point.
Image
Helgef
Posts: 2478
Joined: 17 Jul 2016, 01:02
Contact:

Re: Sort variables

23 Sep 2017, 16:31

Hello, use sort. Eg,

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

loop 5
str.= id%A_Index% "|" v%A_Index% "`n"
sort, str, N ; N is for numeric sort
msgbox % str

Cheers.
User avatar
TLM
Posts: 1146
Joined: 01 Oct 2013, 07:52

Re: Sort variables

23 Sep 2017, 16:40

Helgef wrote:Hello, use sort. Eg,

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

loop 5
str.= id%A_Index% "|" v%A_Index% "`n"
sort, str, N ; N is for numeric sort
msgbox % str

Cheers.
I was also considering sort but then you have to show how the c%number% variables would be assigned.
Image
Helgef
Posts: 2478
Joined: 17 Jul 2016, 01:02
Contact:

Re: Sort variables

23 Sep 2017, 16:51

You have to leave some fun for the op TLM :terms: :D
Spoiler
mast4rwang
Posts: 65
Joined: 19 Jul 2017, 09:59

Re: Sort variables

24 Sep 2017, 10:06

Thank you guys ;)

@TLM I tried to use A_Index method too but I didn't know how to assign values from array to c1 c2 etc, you helped me tons! And yeah, I will use these values later so assigning them is crucial. Until now I only used v1,v2,v3,v4,v5 with random numbers but now I can transform my script into c1 c2 c3 c4 c5 and be assured that my script will read in ascending order(which is a big improvement) :thumbup:


@Helgef I like the simplicity of your example but will I be able to assign the numbers to variables if I am new to arrays? :shock: Of course I'll read about every command you listed in spoilers before giving up 8-)
SirRFI
Posts: 278
Joined: 25 Nov 2015, 16:52

Re: Sort variables

24 Sep 2017, 16:17

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


Code: [Select all] [Download] (result)GeSHi © Codebox Plus

200 = 2
400 = 18
500 = 12
700 = 4
1000 = 7
Use [code=autohotkey] forum tag to share your code.
Click on (Accept this answer) on top-right part of the post if it has answered your question / solved your problem.
mast4rwang
Posts: 65
Joined: 19 Jul 2017, 09:59

Re: Sort variables

25 Sep 2017, 08:55

In the end TLM's answer was the only clear to me. I'm still too noob to play with arrays :D
This is the adjusted TLM's script to do what I need:

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

t::
C1:=5, C2:=4, C3:=3, C4:=2, C5:=1
v1:=C1, v2:=C2, v3:=C3, v4:=C4, v5:=C5
id1:=500, id2:=200, id3:=1000, id4:=700, id5:=10
arr := {}
While ( id%a_index% )
arr[ id%a_index% ] := "v" a_index
For Each, Val in arr
{
c%a_index% := %Val%
}


C values change in the order of sorted ID's and corresponding v's
mast4rwang
Posts: 65
Joined: 19 Jul 2017, 09:59

Re: Sort variables

25 Sep 2017, 09:59

Edit:

Actually the aforementioned method has a bug, the 5th value is being ignored when its value is much lower than the rest. I solved the array thingy like this:

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

Helgef
Posts: 2478
Joined: 17 Jul 2016, 01:02
Contact:

Re: Sort variables  Topic is solved

25 Sep 2017, 10:27

It is enough to sort once, you don't need to sort on every iteration of the loop. Eg,

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

loop,5
str.=id%A_Index% "?" v%A_Index% "`n"
sort, str, N
loop parse, str, `n
{
StringSplit, P, A_LoopField, ? ; You do not need to escape -> ?
A%A_Index% := P2
}

I do not see the issue with small values being ignored, any example?
Cheers.
SirRFI
Posts: 278
Joined: 25 Nov 2015, 16:52

Re: Sort variables

25 Sep 2017, 11:49

You make plenty of variables which turn into a mess. Isn't my sample what You wanted to achieve?
Use [code=autohotkey] forum tag to share your code.
Click on (Accept this answer) on top-right part of the post if it has answered your question / solved your problem.
Helgef
Posts: 2478
Joined: 17 Jul 2016, 01:02
Contact:

Re: Sort variables

25 Sep 2017, 12:11

Sorting by adding values to array as keys is fine, unless there is duplicate values, then you lose data.
mast4rwang
Posts: 65
Joined: 19 Jul 2017, 09:59

Re: Sort variables

26 Sep 2017, 03:20

Helgef wrote:It is enough to sort once, you don't need to sort on every iteration of the loop. Eg,

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

loop,5
str.=id%A_Index% "?" v%A_Index% "`n"
sort, str, N
loop parse, str, `n
{
StringSplit, P, A_LoopField, ? ; You do not need to escape -> ?
A%A_Index% := P2
}

I do not see the issue with small values being ignored, any example?
Cheers.



I just added a little extra condition to avoid getting the 6th value and it works perfectly with this test. Thank you for helping me with mistakes:
Spoiler



As for results with previous script:
Spoiler



@SirRFI
I liked your script a lot but to take variables from it I must learn how to deal with strings better (it is too complicated for me to use it atm) :lol:
Is it possible to have a faster result using your method? I want to check numbers with sleep,1 so the faster the sorter, the faster my whole script will be.
mast4rwang
Posts: 65
Joined: 19 Jul 2017, 09:59

Re: Sort variables

26 Sep 2017, 03:26

@Helgef

I ran your sorter again and found out that it can not deal with repeating values. I can not put it into a loop to check values constantly :)
Helgef
Posts: 2478
Joined: 17 Jul 2016, 01:02
Contact:

Re: Sort variables

26 Sep 2017, 05:34

the smallest value gets pushed into the 5th spot for some reason.

The proposed array methods by TLM and SirRFI will only work with integers, float (and string) keys are sorted alphabetically, not numerically.
I ran your sorter again and found out that it can not deal with repeating values

Example?

I suggest you use str:=rtrim(str, "`n") before the parse loop instead.
mast4rwang
Posts: 65
Joined: 19 Jul 2017, 09:59

Re: Sort variables

26 Sep 2017, 09:43

Helgef wrote:
the smallest value gets pushed into the 5th spot for some reason.

The proposed array methods by TLM and SirRFI will only work with integers, float (and string) keys are sorted alphabetically, not numerically.
I ran your sorter again and found out that it can not deal with repeating values

Example?

I suggest you use str:=rtrim(str, "`n") before the parse loop instead.


Well try to run my test script, it sorts numbers correctly the first time. If you click the button again to repeat loop, it duplicates one value. By the 5th time all values are the same. Maybe it needs to reset string after sorting?
Helgef
Posts: 2478
Joined: 17 Jul 2016, 01:02
Contact:

Re: Sort variables

26 Sep 2017, 11:23

I see, try

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

t::
str:=""
; ...

Cheers :wave:
mast4rwang
Posts: 65
Joined: 19 Jul 2017, 09:59

Re: Sort variables

26 Sep 2017, 11:39

Helgef wrote:I see, try

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

t::
str:=""
; ...

Cheers :wave:


Yeah I thought so :lol: It is now officially finished, cheers!

Return to “Ask For Help”

Who is online

Users browsing this forum: Odlanir, RickC and 41 guests