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.
how to determine if variable exists?
Started by
Richard
, Oct 12 2006 01:48 AM
4 replies to this topic
#1
-
Posted 12 October 2006 - 01:48 AM
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:
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
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
#2
-
Posted 12 October 2006 - 03:04 AM
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.
#3
-
Posted 12 October 2006 - 04:19 AM
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.
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.
#4
-
Posted 12 October 2006 - 08:06 AM
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Dear Richard,
Try the VarExist() function I have posted at http://www.autohotke... ... 2689#82689
Thanks for the idea ..
Regards,
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,
#5
-
Posted 12 October 2006 - 10:19 AM