how to tell if a variable is integer? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
wryyymuda
Posts: 31
Joined: 21 Sep 2018, 21:43

how to tell if a variable is integer?

24 Sep 2018, 17:02

i need to see if a few of the variables in my code are integers and if they are not it just exits the program but i would rather have it state that that's the problem. any function or anything i can put in an if statement to test to see if it is an integer?
wryyymuda
Posts: 31
Joined: 21 Sep 2018, 21:43

Re: how to tell if a variable is integer?

24 Sep 2018, 17:22

it always returns an error and exits the script saying whatever variable i put there is wrong.
User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: how to tell if a variable is integer?  Topic is solved

24 Sep 2018, 17:24

If var is digit

Digit does not accept periods (nor commas), so it works in identifying a value as an integer. However, you may want to incorporate it with another check for it to have a value, as otherwise a null value in the variable is also accepted.

Code: Select all

If !var
{
MsgBox %var% has no value or is false
ExitApp
}
If var is not digit
{
MsgBox %var% is not an integer
ExitApp
}
Unfortunately you can't combine the If var is (not) [type] command with a traditional If expression. You could probably do this though; I haven't tested it:

Code: Select all

If !RegExMatch(var,"$\d+^)
{
MsgBox %var% is not an integer
ExitApp
}
The RegEx here searches for \d characters -- digits 0 through 9. The + means one or more digits must be found. The first matching character must be at the start of the entire haystack, per the $; the last matching character must be at the end of the entire haystack, per the ^ -- in other words, the entire string must be digits throughout.
wryyymuda
Posts: 31
Joined: 21 Sep 2018, 21:43

Re: how to tell if a variable is integer?

24 Sep 2018, 17:26

this is what i am doing

Code: Select all

if def is integer {
msgbox, test
rx = 1
exitapp
}
wryyymuda
Posts: 31
Joined: 21 Sep 2018, 21:43

Re: how to tell if a variable is integer?

24 Sep 2018, 17:27

it also says i can do
if var is not type
but that doesn't work
wryyymuda
Posts: 31
Joined: 21 Sep 2018, 21:43

Re: how to tell if a variable is integer?

24 Sep 2018, 17:40

nevermind you have to combine the is and not.
Rohwedder
Posts: 2353
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: how to tell if a variable is integer?

25 Sep 2018, 03:37

Hallo,
try:

Code: Select all

def = 2.
if def is integer ;One True Brace style not allowed!
{
	MsgBox,,LineNumber: %A_LineNumber%, %def% is integer
}
if def is not integer ;One True Brace style not allowed!
{
	MsgBox,,LineNumber: %A_LineNumber%, %def% is not integer
}
If RegExMatch(def,"^\d+$") { ;One True Brace style allowed
	MsgBox,,LineNumber: %A_LineNumber%, %def% is integer
}
If !RegExMatch(def,"^\d+$") { ;One True Brace style allowed
	MsgBox,,LineNumber: %A_LineNumber%, %def% is not integer
}

Return to “Ask For Help”

Who is online

Users browsing this forum: agrout, Bing [Bot], Google [Bot] and 224 guests