Comparing two variables?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
chef423
Posts: 213
Joined: 28 Aug 2016, 12:10
Contact:

Comparing two variables?

24 May 2018, 16:34

Code: Select all

; Cash Key 2.0
+F11::
	SetTitleMatchMode, 2
	IfWinExist, Cash Register Express -- Station
	{
	Clipboard=
	Send, +{Left 6}
	Send, ^{Ins}
	cl1=
	clipwait,1
	cl1 := clipboard ; Cash amount entering
	Send, {Backspace}
	if Cl1=
	{
	Send, !p
	Send, !c
	Clipboard = 1111
	return
	}
	else
	; Customer presses Credit Debit button
	Clipboard=
	send,!p
	send, {left 8}
	sleep 1000
	send, {DEL}
	sleep 1000
	send, +{right 8}
	sleep 1000
	Send, ^{Ins}
	cl2=
	clipwait,1
	cl2 := clipboard ; Total Invoice
	; Send, {Backspace}
	clipboard=
	; Cancel back to Invoice Screen
	if (cl1 >= cl2)
	{
	sleep 2000
	; send, parta
	send, %cl1%
	sleep 2000
	Send, !c
	cl1=
	cl2=
	return
	}
	else
	{
	; send, partb
	send, %cl1%
	send, !c
	sleep 500
	Send, ^{Ins}
Seems cl1 and cl2 are not comparing as my code always travels thru the if (cl1 >= cl2) part

Am i not writing them correctly via my cl1:=clipboard code?

Thank you in advance for any direction
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Comparing two variables?

24 May 2018, 16:55

Check to see what's in the variables before the condition is evaluated

Code: Select all

;pseudo code
;..........	
	cl2 := clipboard ; Total Invoice
	; Send, {Backspace}
	clipboard=
	; Cancel back to Invoice Screen
	
	msgbox % cl1 " " cl2 ; debug the variables here! 
	if (cl1 >= cl2)
	{
;..........
chef423
Posts: 213
Joined: 28 Aug 2016, 12:10
Contact:

Re: Comparing two variables?

24 May 2018, 17:27

TLM wrote:Check to see what's in the variables before the condition is evaluated

Code: Select all

;pseudo code
;..........	
	cl2 := clipboard ; Total Invoice
	; Send, {Backspace}
	clipboard=
	; Cancel back to Invoice Screen
	
	msgbox % cl1 " " cl2 ; debug the variables here! 
	if (cl1 >= cl2)
	{
;..........
Thanks. My variables are wiring correctly. one has a period in it tho...meaning I enter 1000 (for $10 in the POS) and the cl2 variable that is copied is 20.00...I imagine this is the issue. The comparison is 1000 >< 20.00 which is literal.

Formatting will fix?
chef423
Posts: 213
Joined: 28 Aug 2016, 12:10
Contact:

Re: Comparing two variables?

24 May 2018, 17:34

Actually when I enter a value that should clearly work, the ELSE doesnt trigger (last else in the code). Not sure why my comparison is choking I have verified cl1 and cl2 are collecting the correct values.

Maybe I have to many if else's...hmm
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Comparing two variables?

24 May 2018, 18:50

Well the decimal definitely isn't the issue: msgbox % ( 1000 >= 20.00 ) " " ( 1000 >= 20 )
chef423
Posts: 213
Joined: 28 Aug 2016, 12:10
Contact:

Re: Comparing two variables?

25 May 2018, 09:28

TLM wrote:Well the decimal definitely isn't the issue: msgbox % ( 1000 >= 20.00 ) " " ( 1000 >= 20 )
Yes, it is, that 1000 is actually $10.00, not '1000'. The '1000' amount its converted by the software after entry to $10.00, but when we use the keyboard to enter it,'1000' is the value AHK is reading.
chef423
Posts: 213
Joined: 28 Aug 2016, 12:10
Contact:

Re: Comparing two variables?

25 May 2018, 10:03

The only command I see is Format to convert my '1000' (as an example, if the clerk enters 100 is $1.00, if they enter 75, its .75 cents and so forth), but its not very clear on its use. No way to just cast the INT as 'x.00'?
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Comparing two variables?

25 May 2018, 13:00

What I meant was AHk does not see the difference between 20 & 20.00 for evaluation purposes.
I would divide the whole integer by 100 then either Format or Round the floating point number to 2 decimal places.

Code: Select all

msgbox % ConvertInt( 1000 ) "`n" ConvertInt( 75 ) "`n" ConvertInt( 100 )

ConvertInt( int )
{
	return Format( "{1:0.2f}", int/100 )
}
Feel free to look at math functions that may better suit your needs.
chef423
Posts: 213
Joined: 28 Aug 2016, 12:10
Contact:

Re: Comparing two variables?

25 May 2018, 13:43

TLM wrote:What I meant was AHk does not see the difference between 20 & 20.00 for evaluation purposes.
I would divide the whole integer by 100 then either Format or Round the floating point number to 2 decimal places.

Code: Select all

msgbox % ConvertInt( 1000 ) "`n" ConvertInt( 75 ) "`n" ConvertInt( 100 )

ConvertInt( int )
{
	return Format( "{1:0.2f}", int/100 )
}
Feel free to look at math functions that may better suit your needs.
Thanks. Yes, I looked at the math functions and didn't see anything.
chef423
Posts: 213
Joined: 28 Aug 2016, 12:10
Contact:

Re: Comparing two variables?

25 May 2018, 14:09

TLM wrote:What I meant was AHk does not see the difference between 20 & 20.00 for evaluation purposes.
I would divide the whole integer by 100 then either Format or Round the floating point number to 2 decimal places.

Code: Select all

msgbox % ConvertInt( 1000 ) "`n" ConvertInt( 75 ) "`n" ConvertInt( 100 )

ConvertInt( int )
{
	return Format( "{1:0.2f}", int/100 )
}
Feel free to look at math functions that may better suit your needs.
Good example: ??
; Cash Key 2.0

Code: Select all

+F11::
	SetTitleMatchMode, 2
	IfWinExist, Cash Register Express -- Station
	{
	Clipboard=
	Send, +{Left 6}
	Send, ^{Ins}
	cl1=
	clipwait,1
	cl1 := clipboard ; Cash amount entering
	Send, {Backspace}
        ConvertInt (cl1)
	{
	return Format( "{1:0.2f}", cl1/100 )
	}
	if Cl1=
	{
	Send, !p
	Send, !c
	Clipboard = 1111
	return
	}
	else
	; Customer presses Credit Debit button
	Clipboard=
	send,!p
	send, {left 8}
	sleep 1000
	send, {DEL}
	sleep 1000
	send, +{right 8}
	sleep 1000
	Send, ^{Ins}
	cl2=
	clipwait,1
	cl2 := clipboard ; Total Invoice
	; Send, {Backspace}
	clipboard=
	; Cancel back to Invoice Screen
	if (cl1 >= cl2)
	{
	sleep 2000
	; send, parta
	send, %cl1%
	sleep 2000
	Send, !c
	cl1=
	cl2=
	return
	}
	else
	{
	; send, partb
	send, %cl1%
	send, !c
	sleep 500
	Send, ^{Ins}
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Comparing two variables?

25 May 2018, 16:04

can u, like, reformat your code a little bit? it makes absolutely no sense. this is how it look looks like as of now:

Code: Select all

+F11::
	SetTitleMatchMode, 2
	IfWinExist, Cash Register Express -- Station
	{ ; i wonder if we'll find its closing brace
		Clipboard=
		Send, +{Left 6}
		Send, ^{Ins}
		cl1=
		clipwait,1
		cl1 := clipboard ; Cash amount entering
		Send, {Backspace}

	        ConvertInt (cl1) 					; what is this
		{ 										; and also this, right i get it now
		return Format( "{1:0.2f}", cl1/100 ) 	; youve pasted the function definition right inside the hotkey
		}										; unfortunately, only under very specific circumstances are you allowed to actually do that: see https://autohotkey.com/docs/Hotkeys.htm#Function

		if Cl1=
		{
			Send, !p
			Send, !c
			Clipboard = 1111
			return
		}
		else
		; Customer presses Credit Debit button
			Clipboard= ; ONLY this line is part of the 'else'-statement, so here it is indented to indicate that

		send,!p
		send, {left 8}
		sleep 1000
		send, {DEL}
		sleep 1000
		send, +{right 8}
		sleep 1000
		Send, ^{Ins}
		cl2=
		clipwait,1
		cl2 := clipboard ; Total Invoice
		; Send, {Backspace}
		clipboard=

		; Cancel back to Invoice Screen
		if (cl1 >= cl2)
		{
			sleep 2000
			; send, parta
			send, %cl1%
			sleep 2000
			Send, !c
			cl1=
			cl2=
			return
		}
		else
		{ ; will we find this guy's closing brace 
			; send, partb
			send, %cl1%
			send, !c
			sleep 500
			Send, ^{Ins}
		; and the answer is probably not
	; and the answer to the very first question is also probably not
chef423
Posts: 213
Joined: 28 Aug 2016, 12:10
Contact:

Re: Comparing two variables?

26 May 2018, 13:59

Thanks, I have cleaned it up, ill post it soon..

One thing I need using regexreplace OR stringReplace how to remove $ , . from a INT (a number) and removing the spaces those chataters take up.

So: $1,000.25 would end up 100025

I need to remove this from the cl2 value...

cl2 := StrReplace (cl2, "$ , .", "") - but this doesnt work

RegExReplace better for this?

Thanks,
Chris
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Comparing two variables?

26 May 2018, 14:04

try:

Code: Select all

currency = $1,000.25

msgbox % RegExReplace( currency, "[$,\.]" )
To keep the decimal place use [$,]
It would be nice if StrReplace() had an `array for search text` option like StrSplit() ;)

edit:
BTW if you use the full needle, you can combine the above with the function I provided earlier

Code: Select all

currency 	= $1,000.25
int 		:= RegExReplace( currency, "[$,\.]" )

msgbox % ConvertInt( int )

ConvertInt( int )
{
	return Format( "{1:0.2f}", int/100 )
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: yabab33299 and 125 guests