Cant Get Min To Find Value In Array

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Cant Get Min To Find Value In Array

15 Jun 2021, 08:50

I'm trying to convert a multiline string to an array & then find the minimum value in the array. But min refuses to work on the array ...

Not sure why, as i checked the array & its populated fine

The guide says "If one of the input values is non-numeric, an empty string is returned.", but all the variables are numbers ...

Thanks.,

Code: Select all

;Initialise
#SingleInstance, Force

Every5Seconds:=0
MultilineInc:=0
AddMinFoundInc:=0

Goto Main


;Main

Main:

{

loop, 20

{

MultilineInc++
;tooltip MultilineInc %MultilineInc%
;sleep 100
;FileRead, CurrentPrice1, D:\12\Temp\Price.txt
;CurrentPrice1 := StrReplace(CurrentPrice1,",", "")

;CopyToMultiline%MultilineInc%:=CurrentPrice1

CopyToMultiline1   =20
CopyToMultiline2   =19
CopyToMultiline3   =18
CopyToMultiline4   =17
CopyToMultiline5   =16
CopyToMultiline6   =15
CopyToMultiline7   =14
CopyToMultiline8   =13
CopyToMultiline9   =12
CopyToMultiline10  =11
CopyToMultiline11  =10
CopyToMultiline12  =9
CopyToMultiline13  =8
CopyToMultiline14  =7
CopyToMultiline15  =6
CopyToMultiline16  =5
CopyToMultiline17  =4
CopyToMultiline18  =3
CopyToMultiline19  =2
CopyToMultiline20  =1


	D .= CopyToMultiline%MultilineInc% "`n"

	;tooltip % D
		;D := Trim(D, "`n")

}

a:=D

;Splits String into Array

StringSplit, NewVarArray, a, `n ; Separates a string into an pseudo-array

;tooltip % NewVarArray1


x:= Min(NewVarArray*)

msgbox % x

}


`::
{
Send {esc}
Sleep 100
Send {LControl down}
sleep 100
Send {s}
sleep 100
Send {LControl up}

Reload
}
Return























User avatar
boiler
Posts: 16963
Joined: 21 Dec 2014, 02:44

Re: Cant Get Min To Find Value In Array

15 Jun 2021, 08:56

You have a last item in your array that is blank because you end your string with an extra newline character, and you commented out the part that trims it off.

By the way, there's no need to assign the 20 variables inside the loop. Assigning them like that is not efficient anyway. Not sure why you're doing that.
User avatar
boiler
Posts: 16963
Joined: 21 Dec 2014, 02:44

Re: Cant Get Min To Find Value In Array

15 Jun 2021, 09:01

The trim would not make sense to put inside the loop where you had it. It would need to be after the loop is complete.

Code: Select all

CopyToMultiline1   =20
CopyToMultiline2   =19
CopyToMultiline3   =18
CopyToMultiline4   =17
CopyToMultiline5   =16
CopyToMultiline6   =15
CopyToMultiline7   =14
CopyToMultiline8   =13
CopyToMultiline9   =12
CopyToMultiline10  =11
CopyToMultiline11  =10
CopyToMultiline12  =9
CopyToMultiline13  =8
CopyToMultiline14  =7
CopyToMultiline15  =6
CopyToMultiline16  =5
CopyToMultiline17  =4
CopyToMultiline18  =3
CopyToMultiline19  =2
CopyToMultiline20  =1

loop, 20
	D .= CopyToMultiline%A_Index% "`n"

D := Trim(D, "`n")
NewVarArray := StrSplit(D, "`n")
x:= Min(NewVarArray*)
MsgBox, % x
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Re: Cant Get Min To Find Value In Array

15 Jun 2021, 09:13

Awesome, works fantastic, thanks!
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Re: Cant Get Min To Find Value In Array

15 Jun 2021, 09:40

Hi, I've tried to incorporate the fix into my script, but it still refuses to work ...

Basically every 5 seconds, it adds 5 new strings to the array, & displays the new min, im guessing the new line still isnt being removed correctly ...

Thank!

Code: Select all

;Initialise
#SingleInstance, Force

Every5Seconds:=0
MultilineInc:=0
AddMinFoundInc:=0

Main:


CopyToMultiline1   =20
CopyToMultiline2   =19
CopyToMultiline3   =18
CopyToMultiline4   =17
CopyToMultiline5   =16
CopyToMultiline6   =15
CopyToMultiline7   =14
CopyToMultiline8   =13
CopyToMultiline9   =12
CopyToMultiline10  =11
CopyToMultiline11  =10
CopyToMultiline12  =9
CopyToMultiline13  =8
CopyToMultiline14  =7
CopyToMultiline15  =6
CopyToMultiline16  =5
CopyToMultiline17  =4
CopyToMultiline18  =3
CopyToMultiline19  =2
CopyToMultiline20  =1

MultilineInc++


	D .= CopyToMultiline%A_Index% "`n"
	


If MultilineInc:=20
{
MultilineInc:=0
}

Every5Seconds++
tooltip % Every5Seconds

;5 Seconds
if Every5Seconds =5
{	
Every5Seconds:=0

D := Trim(D, "`n")
NewVarArray := StrSplit(D, "`n")

x:= Min(NewVarArray*)

MsgBox, % x

}
Sleep 500
Goto Main

`::
{
Send {esc}
Sleep 100
Send {LControl down}
sleep 100
Send {s}
sleep 100
Send {LControl up}

Reload
}
Return























User avatar
boiler
Posts: 16963
Joined: 21 Dec 2014, 02:44

Re: Cant Get Min To Find Value In Array

15 Jun 2021, 09:52

You’re using A_Index, which is a built-in variable inside loops. You need to use the variable you are incrementing (MultilineInc) instead.

Also, you are still assigning all 20 variables over and over since it’s inside your Main loop. It doesn’t hurt, but there’s no reason to execute those lines over and over. You should move them before the Move: label.
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Re: Cant Get Min To Find Value In Array

15 Jun 2021, 09:59

Thanks, missed that ...

Ok, now it just shows a single variable 20 & doesnt seem to be incrementing ...

Code: Select all

;Initialise
#SingleInstance, Force

Every5Seconds:=0
MultilineInc:=0
AddMinFoundInc:=0

CopyToMultiline1   =20
CopyToMultiline2   =19
CopyToMultiline3   =18
CopyToMultiline4   =17
CopyToMultiline5   =16
CopyToMultiline6   =15
CopyToMultiline7   =14
CopyToMultiline8   =13
CopyToMultiline9   =12
CopyToMultiline10  =11
CopyToMultiline11  =10
CopyToMultiline12  =9
CopyToMultiline13  =8
CopyToMultiline14  =7
CopyToMultiline15  =6
CopyToMultiline16  =5
CopyToMultiline17  =4
CopyToMultiline18  =3
CopyToMultiline19  =2
CopyToMultiline20  =1

Main:




MultilineInc++


	D .= CopyToMultiline%MultilineInc% "`n"
	


If MultilineInc:=20
{
MultilineInc:=0
}

Every5Seconds++
tooltip % Every5Seconds

;5 Seconds
if Every5Seconds =5
{	
Every5Seconds:=0

D := Trim(D, "`n")
NewVarArray := StrSplit(D, "`n")

x:= Min(NewVarArray*)

MsgBox, % x

}


Sleep 500
Goto Main

`::
{
Send {esc}
Sleep 100
Send {LControl down}
sleep 100
Send {s}
sleep 100
Send {LControl up}

Reload
}
Return
























User avatar
boiler
Posts: 16963
Joined: 21 Dec 2014, 02:44

Re: Cant Get Min To Find Value In Array

15 Jun 2021, 12:12

The first line below sets the variable to 20 because you used the assignment operator := instead of the comparative operator =, which makes the condition true because 20 is non-zero. Since the condition is true, it then assigns it to 0 on the following assignment line. That makes that variable 0 each time through the loop regardless of you incrementing it before that.

Code: Select all

If MultilineInc:=20
{
MultilineInc:=0
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb, Google [Bot], Joey5 and 174 guests