Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

how to determine if variable exists?


  • Please log in to reply
4 replies to this topic
Richard
  • Guests
  • Last active:
  • Joined: --
Is there a way in autohotkey to determine whether a certain variable exists?

I would like to be able to tell the difference between a variable that does not exist and a variable that exists, but has an empty-string for a value.

For example, I have a script in which I take some text from a file and attempt to replace certain words with the value of the variable that corresponds to each word, if such a variable exists.

For example:
Text = Dear User, please open the [programfiles] folder and open [WeirdName].

My script will search for words in brackets and replace [programfiles] with "C:\program files" etc. because %ProgramFiles% is a valid, existing variable. But it will not replace [WeirdName], assuming that no variable is called "WeirdName"
Thus the resulting line will read:
"Dear User, please open the C:\Program Files folder and open [WeirdName]"

Usually, you might think that testing the variable for blank would suffice. If the variable is blank, then it probably doesn't exist, so don't make the replacement.
Sometimes however I would like to actually insert a blank if there is a variable with the right name.

For example:
Text = Please [test] eat the apple

The variable %test% could be set to "do not" in which case the resulting line would read "Please do not eat the apple"
If %test% = "" (blank) , the resulting line would be "Please eat the apple"

Now of course, my script is much more complicated. The variables can be defined in various other included scripts, so I don't know which variables exist until I test their values. Futhermore, the text can have several items in brackets that should be taken litterally (i.e. there are NO variables that the words inside them correspond to)

So I really do need to tell whether each word exists as a variable (even if the value of the variable is blank)

Thanks very much for any help you can give.

Lemming
  • Members
  • 184 posts
  • Last active: Feb 03 2014 11:03 AM
  • Joined: 20 Dec 2005
From the docs:

Variables are not declared; they come into existence simply by using them (and each variable starts off empty/blank).

So in your case, there is no need to test for empty variables:

Text = Please %VarName%eat the apple

If VarName is blank, the result would be "Please eat the apple". Note the spaces though; you'll probably want to read up on the AutoTrim command too.

-Lemming

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
There is a way to determine if a variable exists: ListVars. With some tricks you can keep it hidden, but still read the content of its edit control, which contains the list of all the variables. With AHK's string manipulation routines you can tell if a name is in there.

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
It would be interesting, sometime, to have programmatical access to these debug tools:
lv := ListVars
kh := KeyHistory
lh := ListHotkeys
ll := ListLines

The data is here, making it available should be so hard.

Note: this is another point where the lack of nil/null shows, to distinguish between an empty var and a uninitialized one. Idem for parameters' default value, we usually use "" as default value to see when it is omitted, but sometime this is a valid value. These nil/null values can sometime trip the developper, but overall, they are useful.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Dear Richard, :)

Is there a way in autohotkey to determine whether a certain variable exists?
I would like to be able to tell the difference between a variable that does not exist and a variable that exists, but has an empty-string for a value.


Try the VarExist() function I have posted at http://www.autohotke... ... 2689#82689

Thanks for the idea ..

Regards, :)
kWo4Lk1.png