IF var is varname

Propose new features and changes
joefiesta
Posts: 497
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

IF var is varname

23 Jul 2017, 10:25

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!
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: IF var is varname

23 Jul 2017, 13:54

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

User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: IF var is varname

23 Jul 2017, 14:23

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.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
joefiesta
Posts: 497
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: IF var is varname

08 Sep 2017, 09:07

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!
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: IF var is varname

11 Sep 2017, 11:07

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?"

User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: IF var is varname

11 Sep 2017, 11:24

@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.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 28 guests