how to tell if a variable is integer? Topic is solved
how to tell if a variable is integer?
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?
-
- Posts: 1472
- Joined: 05 May 2018, 12:23
Re: how to tell if a variable is integer?
it always returns an error and exits the script saying whatever variable i put there is wrong.AHKStudent wrote:https://autohotkey.com/docs/commands/IfIs.htm
Re: how to tell if a variable is integer? Topic is solved
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.
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:
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.
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
}
Code: Select all
If !RegExMatch(var,"$\d+^)
{
MsgBox %var% is not an integer
ExitApp
}
Re: how to tell if a variable is integer?
this is what i am doing
Code: Select all
if def is integer {
msgbox, test
rx = 1
exitapp
}
Re: how to tell if a variable is integer?
it also says i can do
if var is not type
but that doesn't work
if var is not type
but that doesn't work
Re: how to tell if a variable is integer?
nevermind you have to combine the is and not.
Re: how to tell if a variable is integer?
Hallo,
try:
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
}
Who is online
Users browsing this forum: Rohwedder and 120 guests