I would like to propose an addition to the list of "variable types" to be checked for by the IF VAR IS TYPE command.
I think it would be nice if AHK could check for a variable value being a valid variable name, e.g.
IF myvar IS TYPE VARNAME
Thanks
Joe Petree
p.s. yes, this is quite easy for some, especially RegEx experts, but (1) the valid values may depend on OS or AHK platform and (2) we're not all RegEx experts!
IF var is varname
Re: IF var is varname
from
https://autohotkey.com/docs/Variables.htm#Variables
https://autohotkey.com/docs/misc/RegEx-QuickRef.htm
https://autohotkey.com/docs/Variables.htm#Variables
so:Variable names may be up to 253 characters long and may consist of letters, numbers and the following punctuation: # _ @ $
Code: Select all
myvar := "high-five"
if RegExMatch(myvar, "^[a-zA-Z0-9#_@\$]{1,253}$")
msgbox, %myvar% is a valid variable name
else
msgbox, %myvar% is not a valid variable name
Re: IF var is varname
It seems that AHK v2 has tightened up the rules:
v2-changes
https://autohotkey.com/v2/v2-changes.htm
if RegExMatch(myvar, "^[a-zA-Z_]\w{0,252}$")
or if you also allowed the non-ASCII characters (which I personally wouldn't do):
if RegExMatch(myvar, "^([a-zA-Z_]|[^[:ascii:]])(\w|[^[:ascii:]]){0,252}$")
I initially thought you were asking about a 'VarExist' or 'IsVar' function, which could be interesting.
v2-changes
https://autohotkey.com/v2/v2-changes.htm
So I believe the RegEx would be:Variable names cannot start with a digit and cannot contain the following characters which were previously allowed: @ # $. Only letters, numbers, underscore and non-ASCII characters are allowed.
if RegExMatch(myvar, "^[a-zA-Z_]\w{0,252}$")
or if you also allowed the non-ASCII characters (which I personally wouldn't do):
if RegExMatch(myvar, "^([a-zA-Z_]|[^[:ascii:]])(\w|[^[:ascii:]]){0,252}$")
I initially thought you were asking about a 'VarExist' or 'IsVar' function, which could be interesting.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: IF var is varname
Thanks for the Regex answers. However, Jeeswg hit the nail on the head. Not with his exactness (thank you), but with the rationale.
Since V1 and V2 require different code to test a string for validity as a fileid, that is all the MORE REASON why "If ... is varname" would be a good candidate for a new command. WHO WANTS TO WRITE CODE THAT IS VERSION DEPENDENT? Yuck. I still have Xedit macros (an IBM mainframe editor) written in the early 80's that run fine!
Since V1 and V2 require different code to test a string for validity as a fileid, that is all the MORE REASON why "If ... is varname" would be a good candidate for a new command. WHO WANTS TO WRITE CODE THAT IS VERSION DEPENDENT? Yuck. I still have Xedit macros (an IBM mainframe editor) written in the early 80's that run fine!
Re: IF var is varname
No one, but that's getting harder and harder to do, since the explicit goal of AHK v2 is to make the changes that BREAK COMPATIBILITY.joefiesta wrote:WHO WANTS TO WRITE CODE THAT IS VERSION DEPENDENT?
So your question would be akin to asking, "Who wants to write code that doesn't work on both AHK and AutoIT?"
Re: IF var is varname
@joefiesta: In this case, an answer could be a version that works in both versions, taking the restrictions of both: the AHK v1 restrictions: no non-ASCII characters and only certain ASCII characters, the AHK v2 restrictions: can't start with a digit, only allowed letters/digits/underscore/non-ASCII characters:
Is this useful to you? I've never needed a function like this myself. However, it may be useful in future, if I or other people work on some AutoHotkey script parsers/error checkers.
Btw does it say anywhere the restriction for function names. Are the variable/function name rules, the same rules? I tested and: AHK v1 allows function names to start with a digit, AHK v2 doesn't.
Code: Select all
if RegExMatch(myvar, "^[a-zA-Z_]\w{0,252}$")
Btw does it say anywhere the restriction for function names. Are the variable/function name rules, the same rules? I tested and: AHK v1 allows function names to start with a digit, AHK v2 doesn't.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Who is online
Users browsing this forum: No registered users and 27 guests