Sort variables Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
mast4rwang
Posts: 141
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

Variables to sort:
v1:=12
v2:=2
v3:=7
v4:=4
v5:=18

Values used for sorting. id1 represents v1, id2 represents v2 and so on.
id1:=500
id2:=200
id3:=1000
id4:=700
id5:=400

200->400->500->700->1000
so...
id2 -> id5 -> id1 -> id4 ->id3
so...
v2 -> v5 -> v1 -> v4 -> v3

Expected result:
c1:= v2
c2:= v5
c3:= v1
c4:= v4
c5:= v3
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: 1608
Joined: 01 Oct 2013, 07:52
Contact:

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

v1:=12
v2:=2
v3:=7
v4:=4
v5:=18

; Values used for sorting. id1 represents v1, id2 represents v2 and so on.

id1:=500
id2:=200
id3:=1000
id4:=700
id5:=400

arr := {}
While ( id%a_index% )
	arr[ id%a_index% ] := "v" a_index

For Each, Val in arr ; only works for ascending order
{
	c%a_index% := Val     ; assign the variable id%number% to pseudo array / variable c%number% in order 
	displaystring .= Val "->"
}

msgbox % Trim( displaystring, "->" ) "`nThe value of C3 is: " C3
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.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Sort variables

23 Sep 2017, 16:31

Hello, use sort. Eg,

Code: Select all

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: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Sort variables

23 Sep 2017, 16:40

Helgef wrote:Hello, use sort. Eg,

Code: Select all

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.
Helgef
Posts: 4709
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: 141
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: 404
Joined: 25 Nov 2015, 16:52

Re: Sort variables

24 Sep 2017, 16:17

Code: Select all

HowAboutThis :=
(Join QC
{
	;ID:	V
	500:	12,
	200:	2,
	1000:	7,
	700:	4,
	400:	18
}
)

Result := ""
For ID,V in HowAboutThis	; Your "c" thingy
	Result .= ID " = " V "`n"
MsgBox % Result

Code: Select all

200 = 2
400 = 18
500 = 12
700 = 4
1000 = 7
Use

Code: Select all

[/c] forum tag to share your code.
Click on [b]✔[/b] ([b][i]Accept this answer[/i][/b]) on top-right part of the post if it has answered your question / solved your problem.
mast4rwang
Posts: 141
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

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: 141
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

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
loop,5
{
str.=id%A_Index% "?" v%A_Index% "?" "`n" 
sort, str, N
}
StringSplit, P, str, `?
A1:=P2
A2:=P4
A3:=P6
A4:=P8
A5:=P10
str:=""
msgbox, %A1%, %A2%, %A3%, %A4%, %A5%
exitapp
Helgef
Posts: 4709
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

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: 404
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: Select all

[/c] forum tag to share your code.
Click on [b]✔[/b] ([b][i]Accept this answer[/i][/b]) on top-right part of the post if it has answered your question / solved your problem.
Helgef
Posts: 4709
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: 141
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

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: 141
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: 4709
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?

Code: Select all

if (A_Index > 5)
break
I suggest you use str:=rtrim(str, "`n") before the parse loop instead.
mast4rwang
Posts: 141
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?

Code: Select all

if (A_Index > 5)
break
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: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Sort variables

26 Sep 2017, 11:23

I see, try

Code: Select all

t::
	str:=""
	; ...
Cheers :wave:
mast4rwang
Posts: 141
Joined: 19 Jul 2017, 09:59

Re: Sort variables

26 Sep 2017, 11:39

Helgef wrote:I see, try

Code: Select all

t::
	str:=""
	; ...
Cheers :wave:
Yeah I thought so :lol: It is now officially finished, cheers!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], mikeyww, mmflume and 158 guests