Page 1 of 1

IF var is varname

Posted: 23 Jul 2017, 10:25
by joefiesta
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!

Re: IF var is varname

Posted: 23 Jul 2017, 13:54
by guest3456
from
https://autohotkey.com/docs/Variables.htm#Variables
Variable names may be up to 253 characters long and may consist of letters, numbers and the following punctuation: # _ @ $
so:

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
https://autohotkey.com/docs/misc/RegEx-QuickRef.htm

Re: IF var is varname

Posted: 23 Jul 2017, 14:23
by jeeswg
It seems that AHK v2 has tightened up the rules:
v2-changes
https://autohotkey.com/v2/v2-changes.htm
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.
So I believe the RegEx would be:
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.

Re: IF var is varname

Posted: 08 Sep 2017, 09:07
by joefiesta
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!

Re: IF var is varname

Posted: 11 Sep 2017, 11:07
by guest3456
joefiesta wrote:WHO WANTS TO WRITE CODE THAT IS VERSION DEPENDENT?
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.

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

Posted: 11 Sep 2017, 11:24
by jeeswg
@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:

Code: Select all

if RegExMatch(myvar, "^[a-zA-Z_]\w{0,252}$")
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.