AHK has IsByRef(), IsFunc(), IsLabel() and IsObject(). But, I need IsVar().
How can I tell if a script has initialized a given variable?
How to Validate if a variable has been initialized
Re: How to Validate if a variable has been initialized
You could have a look at ListVars. I believe that a few people have posted some tricks to copy the list.
https://www.autohotkey.com/boards/viewtopic.php?t=14
https://www.autohotkey.com/boards/viewtopic.php?t=14
Re: How to Validate if a variable has been initialized
Thanks. I know listvars shows initialized variables. But, I want to do it within my script, without having to "futz" with listvars or someting. Sounds like the answer is: you can't. Kinda disappointing. And, I looked at DEBUGVARS.ahk by lexicos. Way too much and way over my head even if I could modify it to give me what I want.
Oh, well. i can ASSUME any variable that is null hasn't been initialized. But, I might go to programmer's hell for making assumptions.
Oh, well. i can ASSUME any variable that is null hasn't been initialized. But, I might go to programmer's hell for making assumptions.
Re: How to Validate if a variable has been initialized
Since you create variables simply by using them in AHK, already existing variables can be blank.
In fact, they start blank until they get assigned something else.
Edit:
Of course, you won't know if they held some value at some point before. So I wouldn't assume this.
In fact, they start blank until they get assigned something else.
Edit:
But if you only consider non-blank variables initialized, then just check if they are non-blank.https://www.autohotkey.com/docs/Concepts.htm#variables wrote:In AutoHotkey, variables are created simply by using them. Each variable is not permanently restricted to a single data type, but can instead hold a value of any type: string, number or object. Each variable starts off empty/blank; in other words, each newly created variable contains an empty string until it is assigned some other value.
Of course, you won't know if they held some value at some point before. So I wouldn't assume this.
Re: How to Validate if a variable has been initialized
Code: Select all
var1 := 123
MsgBox, % IsInitialized(var1) ? "Initialized" : "Not initialized"
MsgBox, % IsInitialized(var2) ? "Initialized" : "Not initialized"
var1 := "" ; has been initialized, though now emptied
var2 := "" ; still never initialized
MsgBox, % IsInitialized(var1) ? "Initialized" : "Not initialized"
MsgBox, % IsInitialized(var2) ? "Initialized" : "Not initialized"
IsInitialized(ByRef var) {
return &var != &UninitializedVar
}
-
- Posts: 5
- Joined: 13 Apr 2021, 07:35
Re: How to Validate if a variable has been initialized
Would something like this work?
Code: Select all
IsVar(varinquestion) {
varinquestion := !varinquestion
if (varinquestion = 0)
return true
}
Re: How to Validate if a variable has been initialized
Good idea Seems like the way to go.boiler wrote: ↑21 Apr 2021, 14:08Credit to @Xtra for noting that address of uninitialized variables are all the same.Code: Select all
var1 := 123 MsgBox, % IsInitialized(var1) ? "Initialized" : "Not initialized" MsgBox, % IsInitialized(var2) ? "Initialized" : "Not initialized" var1 := "" ; has been initialized, though now emptied var2 := "" ; still never initialized MsgBox, % IsInitialized(var1) ? "Initialized" : "Not initialized" MsgBox, % IsInitialized(var2) ? "Initialized" : "Not initialized" IsInitialized(ByRef var) { return &var != &UninitializedVar }
Re: How to Validate if a variable has been initialized
It only tells you if it is empty or not:hidden_relic wrote: ↑21 Apr 2021, 14:09Would something like this work?
Code: Select all
IsVar(varinquestion) { varinquestion := !varinquestion if (varinquestion = 0) return true }
Code: Select all
var1 := 123
MsgBox, % IsVar(var1) ? "Initialized" : "Not initialized"
MsgBox, % IsVar(var2) ? "Initialized" : "Not initialized"
var1 := "" ; has been initialized, though now emptied
var2 := "" ; still never initialized
MsgBox, % IsVar(var1) ? "Initialized" : "Not initialized" ; says "Not initialized" even though it was once (i.e., it has a unique address)
MsgBox, % IsVar(var2) ? "Initialized" : "Not initialized"
IsVar(varinquestion) {
varinquestion := !varinquestion
if (varinquestion = 0)
return true
}
-
- Posts: 5
- Joined: 13 Apr 2021, 07:35
Re: How to Validate if a variable has been initialized
maybe i am just being spacey, but I don't understand the difference
boiler wrote: ↑21 Apr 2021, 14:13It only tells you if it is empty or not:hidden_relic wrote: ↑21 Apr 2021, 14:09Would something like this work?
Code: Select all
IsVar(varinquestion) { varinquestion := !varinquestion if (varinquestion = 0) return true }
Code: Select all
var1 := 123 MsgBox, % IsVar(var1) ? "Initialized" : "Not initialized" MsgBox, % IsVar(var2) ? "Initialized" : "Not initialized" var1 := "" ; has been initialized, though now emptied var2 := "" ; still never initialized MsgBox, % IsVar(var1) ? "Initialized" : "Not initialized" ; says "Not initialized" even though it was once (i.e., it has a unique address) MsgBox, % IsVar(var2) ? "Initialized" : "Not initialized" IsVar(varinquestion) { varinquestion := !varinquestion if (varinquestion = 0) return true }
Re: How to Validate if a variable has been initialized
Your version says var1 has not been initialized even though it was, only because its contents have been emptied. A variable can be initialized and then be assigned a null value, as is the case here, but your function identifies it as not being a variable or an initialized variable, however you want to put it. The version that @Xtra and I posted will show if a variable exists (has an actual unique address and has had a value) regardless of its current contents.
-
- Posts: 5
- Joined: 13 Apr 2021, 07:35
Re: How to Validate if a variable has been initialized
thank you for explaining
boiler wrote: ↑21 Apr 2021, 14:24Your version says var1 has not been initialized even though it was, only because its contents have been emptied. A variable can be initialized and then be assigned a null value, as is the case here, but your function identifies it as not being a variable or an initialized variable, however you want to put it. The version that @Xtra and I posted will show if a variable exists (has an actual unique address and has had a value) regardless of its current contents.
Re: How to Validate if a variable has been initialized
thanks. Boiler's solution is rather cool!
But, unfortunately, what I REALLY wanted--and, yes, I probably misworded my question--is: How can I tell if a variable has been USED in a script? That is, can there be a way to distinguish betweenthe two "Not initialized" responses in regards to VAR2.
I'm probably asking for too much from what I can gather.
But, unfortunately, what I REALLY wanted--and, yes, I probably misworded my question--is: How can I tell if a variable has been USED in a script? That is, can there be a way to distinguish betweenthe two "Not initialized" responses in regards to VAR2.
I'm probably asking for too much from what I can gather.
Re: How to Validate if a variable has been initialized
Then I would parse lexikos' Scriptinfo("ListVars") function: https://www.autohotkey.com/boards/viewtopic.php?f=6&t=9656&p=244893
As soon as afunction variable gets used (created, but not necessarily assigned), it should show up there.
You could also wrap that into a function of your own.
As soon as a
You could also wrap that into a function of your own.
Last edited by gregster on 21 Apr 2021, 15:08, edited 1 time in total.
Reason: Of course, it should read 'variable'.
Reason: Of course, it should read 'variable'.
Re: How to Validate if a variable has been initialized
How do you mean used? If you assign nothing (null) to a variable, it still hasn’t been initialized or “used” in the script yet. It actually doesn’t exist as far as AHK is concerned.
Edit: Does that ScriptInfo show a variable that has only been assigned null in the list of variables?
Edit: Does that ScriptInfo show a variable that has only been assigned null in the list of variables?
Last edited by boiler on 21 Apr 2021, 14:57, edited 1 time in total.
Re: How to Validate if a variable has been initialized
afaics, ListVars sees it differently.
Edit: Scriptinfo, too. It shows the same info like the built-in AHK commands.
Code: Select all
msgbox % Var
listvars
msgbox
Re: How to Validate if a variable has been initialized
OK. Good to know.
Re: How to Validate if a variable has been initialized
In fact, it seems, Listvars knows already about the existence of blank variables later in the code:
It probably scans the whole code each time it is called.
Edit: or more likely all non-dynamic variables are already created as blanks at script start and Listvars just goes through that list.
Code: Select all
listvars
sleep, 5000
msgbox % var2
a::msgbox % var4
Edit: or more likely all non-dynamic variables are already created as blanks at script start and Listvars just goes through that list.
Last edited by gregster on 21 Apr 2021, 15:30, edited 1 time in total.
Reason: Edit
Reason: Edit
Re: How to Validate if a variable has been initialized
Interesting thanks for pointing that out gregster.
Re: How to Validate if a variable has been initialized
I would doubt LISTVARS scans the code. I would think he reads the symbol table. Otherwise, how can you explain him showing VAR100 in the following:
z := 100
var%z% := 666
listvars
msgbox holding listvars display ...
z := 100
var%z% := 666
listvars
msgbox holding listvars display ...
Who is online
Users browsing this forum: bobstoner289, Google [Bot], Ralf_Reddings200244 and 293 guests