How to Validate if a variable has been initialized

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
joefiesta
Posts: 497
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

How to Validate if a variable has been initialized

Post by joefiesta » 21 Apr 2021, 13:33

AHK has IsByRef(), IsFunc(), IsLabel() and IsObject(). But, I need IsVar().

How can I tell if a script has initialized a given variable?
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: How to Validate if a variable has been initialized

Post by mikeyww » 21 Apr 2021, 13:40

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
gregster
Posts: 8989
Joined: 30 Sep 2013, 06:48

Re: How to Validate if a variable has been initialized

Post by gregster » 21 Apr 2021, 13:44

mikeyww wrote:
21 Apr 2021, 13:40
I believe that a few people have posted some tricks to copy the list.
Or this: https://www.autohotkey.com/boards/viewtopic.php?f=6&t=9656&p=244893
joefiesta
Posts: 497
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: How to Validate if a variable has been initialized

Post by joefiesta » 21 Apr 2021, 14:01

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.
gregster
Posts: 8989
Joined: 30 Sep 2013, 06:48

Re: How to Validate if a variable has been initialized

Post by gregster » 21 Apr 2021, 14:04

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:
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.
But if you only consider non-blank variables initialized, then just check if they are non-blank.
Of course, you won't know if they held some value at some point before. So I wouldn't assume this.
User avatar
boiler
Posts: 16901
Joined: 21 Dec 2014, 02:44

Re: How to Validate if a variable has been initialized

Post by boiler » 21 Apr 2021, 14:08

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
}
Credit to @Xtra for noting that address of uninitialized variables are all the same.
hidden_relic
Posts: 5
Joined: 13 Apr 2021, 07:35

Re: How to Validate if a variable has been initialized

Post by hidden_relic » 21 Apr 2021, 14:09

Would something like this work?

Code: Select all

IsVar(varinquestion) {
varinquestion := !varinquestion
if (varinquestion = 0)
return true
}
gregster
Posts: 8989
Joined: 30 Sep 2013, 06:48

Re: How to Validate if a variable has been initialized

Post by gregster » 21 Apr 2021, 14:10

boiler wrote:
21 Apr 2021, 14:08

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
}
Credit to @Xtra for noting that address of uninitialized variables are all the same.
Good idea :thumbup: Seems like the way to go.
User avatar
boiler
Posts: 16901
Joined: 21 Dec 2014, 02:44

Re: How to Validate if a variable has been initialized

Post by boiler » 21 Apr 2021, 14:13

hidden_relic wrote:
21 Apr 2021, 14:09
Would something like this work?

Code: Select all

IsVar(varinquestion) {
varinquestion := !varinquestion
if (varinquestion = 0)
return true
}
It only tells you if it is empty or not:

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
}
hidden_relic
Posts: 5
Joined: 13 Apr 2021, 07:35

Re: How to Validate if a variable has been initialized

Post by hidden_relic » 21 Apr 2021, 14:17

maybe i am just being spacey, but I don't understand the difference
boiler wrote:
21 Apr 2021, 14:13
hidden_relic wrote:
21 Apr 2021, 14:09
Would something like this work?

Code: Select all

IsVar(varinquestion) {
varinquestion := !varinquestion
if (varinquestion = 0)
return true
}
It only tells you if it is empty or not:

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
}
User avatar
boiler
Posts: 16901
Joined: 21 Dec 2014, 02:44

Re: How to Validate if a variable has been initialized

Post by boiler » 21 Apr 2021, 14:24

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.
hidden_relic
Posts: 5
Joined: 13 Apr 2021, 07:35

Re: How to Validate if a variable has been initialized

Post by hidden_relic » 21 Apr 2021, 14:26

thank you for explaining
boiler wrote:
21 Apr 2021, 14:24
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.
joefiesta
Posts: 497
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: How to Validate if a variable has been initialized

Post by joefiesta » 21 Apr 2021, 14:49

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.
gregster
Posts: 8989
Joined: 30 Sep 2013, 06:48

Re: How to Validate if a variable has been initialized

Post by gregster » 21 Apr 2021, 14:53

Then I would parse lexikos' Scriptinfo("ListVars") function: https://www.autohotkey.com/boards/viewtopic.php?f=6&t=9656&p=244893
As soon as a function variable gets used (created, but not necessarily assigned), it should show up there.
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'.
User avatar
boiler
Posts: 16901
Joined: 21 Dec 2014, 02:44

Re: How to Validate if a variable has been initialized

Post by boiler » 21 Apr 2021, 14:56

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?
Last edited by boiler on 21 Apr 2021, 14:57, edited 1 time in total.
gregster
Posts: 8989
Joined: 30 Sep 2013, 06:48

Re: How to Validate if a variable has been initialized

Post by gregster » 21 Apr 2021, 14:57

afaics, ListVars sees it differently.

Code: Select all

msgbox % Var
listvars
msgbox
var.png
var.png (533 Bytes) Viewed 1301 times
Edit: Scriptinfo, too. It shows the same info like the built-in AHK commands.
User avatar
boiler
Posts: 16901
Joined: 21 Dec 2014, 02:44

Re: How to Validate if a variable has been initialized

Post by boiler » 21 Apr 2021, 14:58

OK. Good to know.
gregster
Posts: 8989
Joined: 30 Sep 2013, 06:48

Re: How to Validate if a variable has been initialized

Post by gregster » 21 Apr 2021, 15:17

In fact, it seems, Listvars knows already about the existence of blank variables later in the code:

Code: Select all

listvars
sleep, 5000
msgbox % var2

a::msgbox % var4
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.
Last edited by gregster on 21 Apr 2021, 15:30, edited 1 time in total.
Reason: Edit
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: How to Validate if a variable has been initialized

Post by Xtra » 21 Apr 2021, 15:21

Interesting thanks for pointing that out gregster.
joefiesta
Posts: 497
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: How to Validate if a variable has been initialized

Post by joefiesta » 21 Apr 2021, 15:32

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 ...
Post Reply

Return to “Ask for Help (v1)”