| View previous topic :: View next topic |
| Author |
Message |
Autex675
Joined: 10 May 2008 Posts: 15
|
Posted: Tue May 27, 2008 10:00 pm Post subject: Checking variable existance? |
|
|
How can I check if a variable exists without producing an error?
For example, this code
| Code: | if %var!% =
msgbox, "var!" exists
|
does not work since "var!" has an invalid character in it. |
|
| Back to top |
|
 |
engunneer
Joined: 30 Aug 2005 Posts: 6847 Location: Pacific Northwest, US
|
Posted: Tue May 27, 2008 11:29 pm Post subject: |
|
|
note, checking if a variable exists by using
actually will create the variable.
do you want to know if the variable has been used, or just that it has data in it?
| Code: |
if var =
msgbox, var does not have data
;or
if not var
msgbox, var does not have data
|
_________________
Unless otherwise noted, all code is untested.
Common Answers: 1.(Loops, Viruses, etc.) 2. Search 3.RTFM |
|
| Back to top |
|
 |
Autex675
Joined: 10 May 2008 Posts: 15
|
Posted: Wed May 28, 2008 2:29 am Post subject: Checking variable existance? |
|
|
| Part of one of my scripts sometimes tries to refer to an invalid variable such as "V+a+r" ("+" cannot be use in a variable name). How can I make the script check to see if the variable is valid with causing an error? |
|
| Back to top |
|
 |
[VxE]
Joined: 07 Oct 2006 Posts: 1494
|
Posted: Wed May 28, 2008 3:02 am Post subject: |
|
|
| Code: | ; A valid variable name will be returned unchanged
; An invalid varaible name will yield an empty string
IsValidVariableName( VarNameString ) ; by [VxE]
{ ; checks a string to see if AHK will accept it as a valid variable name
; list of valid characters taked from the AHK manual
static ValidChars := "#_@$?[]abcdefghijklmnopqrstuvwxyz1234567890"
Loop, Parse, VarNameString
If !InStr(ValidChars, A_LoopField)
VarNameString := ""
return VarNameString
} |
maybe this? _________________ My Home Thread
More Common Answers: [1]. It's in the FAQ [2]. Ternary ( a ? b : c ) guide [3]. Post code inside [code][/code] tags ! |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 2737 Location: Australia, Qld
|
Posted: Wed May 28, 2008 11:32 am Post subject: |
|
|
| Code: | MsgBox % IsValidVariableName("var1")
MsgBox % IsValidVariableName("var!")
IsValidVariableName(VarNameString) {
return !RegExMatch(VarNameString, "[^\w#@$?\[\]]")
} |
|
|
| Back to top |
|
 |
Autex675
Joined: 10 May 2008 Posts: 15
|
Posted: Thu May 29, 2008 7:50 pm Post subject: Cool Script |
|
|
Hi, [VxE]
That script is cool!
Tkanks
P.S. I have an updated version of the script.
| Code: |
IsValidVariableName(byref VarNameString)
{
static ValidChars
ValidChars := "#_@$?[]abcdefghijklmnopqrstuvwxyz1234567890"
Loop, Parse, VarNameString
{
If (not InStr(ValidChars, A_LoopField))
VarNameString := ""
}
return VarNameString
}
|
|
|
| Back to top |
|
 |
|