AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Checking variable existance?

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Autex675



Joined: 10 May 2008
Posts: 15

PostPosted: Tue May 27, 2008 10:00 pm    Post subject: Checking variable existance? Reply with quote

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
View user's profile Send private message
engunneer



Joined: 30 Aug 2005
Posts: 6847
Location: Pacific Northwest, US

PostPosted: Tue May 27, 2008 11:29 pm    Post subject: Reply with quote

note, checking if a variable exists by using
Code:

if (variable)
...

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
View user's profile Send private message Visit poster's website
Autex675



Joined: 10 May 2008
Posts: 15

PostPosted: Wed May 28, 2008 2:29 am    Post subject: Checking variable existance? Reply with quote

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
View user's profile Send private message
[VxE]



Joined: 07 Oct 2006
Posts: 1494

PostPosted: Wed May 28, 2008 3:02 am    Post subject: Reply with quote

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
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 2737
Location: Australia, Qld

PostPosted: Wed May 28, 2008 11:32 am    Post subject: Reply with quote

Code:
MsgBox % IsValidVariableName("var1")
MsgBox % IsValidVariableName("var!")
IsValidVariableName(VarNameString) {
    return !RegExMatch(VarNameString, "[^\w#@$?\[\]]")
}
Back to top
View user's profile Send private message
Autex675



Joined: 10 May 2008
Posts: 15

PostPosted: Thu May 29, 2008 7:50 pm    Post subject: Cool Script Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group