Wenfang Du
Wenfang Du

Reputation: 11317

How to check if a variable has been assigned a value in AutoHotkey?

#Warn

WinActivate ahk_class %PrevActiveClass%

When running the above code, the interpreter throws:

enter image description here

I want to check if PrevActiveClass has been assigned a value, if it has, then run WinActivate, how to implement this logic in AutoHotkey?

Upvotes: 1

Views: 2965

Answers (3)

Wenfang Du
Wenfang Du

Reputation: 11317

Quote from #Warn:

Enables or disables warnings for specific conditions which may indicate an error, such as a typo or missing "global" declaration.

#Warn [WarningType, WarningMode]

WarningType

The type of warning to enable or disable. If omitted, it defaults to All.

UseUnsetLocal or UseUnsetGlobal: Warn when a variable is read without having previously been assigned a value or initialized with VarSetCapacity(). If the variable is intended to be empty, assign an empty string to suppress this warning.

This is split into separate warning types for locals and globals because it is more common to use a global variable without prior initialization, due to their persistent and script-wide nature. For this reason, some script authors may wish to enable this type of warning for locals but disable it for globals. [emphasis added]

#Warn
; y := "" ; This would suppress the warning.
x := y ; y hasn't been assigned a value.

In this case, it's safe to turn off the warning for UseUnsetGlobal:

#Warn
#Warn UseUnsetGlobal, Off

WinActivate ahk_class %PrevActiveClass%

Upvotes: 0

0x464e
0x464e

Reputation: 6489

If you actually want to check whether or not a variable has been set, you can do that by comparing its address(docs) to the address of a non-existent variable:

var1 := ""      ;will return non-existent, this doesn't actually really create a variable
var2 := "hello" ;will return non-empty
var3 := "test"  ;now a variable actually gets created
var3 := ""      ;but here we set it to be empty, so it'll return empty
var4 := false   ;will return non-empty
;var5           ;will return non-existent


MsgBox, % (&Var1 = &NonExistentVar ? "Non-existent" : (Var1 = "" ? "Empty" : "Non-empty")) "`n"
        . (&Var2 = &NonExistentVar ? "Non-existent" : (Var2 = "" ? "Empty" : "Non-empty")) "`n"
        . (&Var3 = &NonExistentVar ? "Non-existent" : (Var3 = "" ? "Empty" : "Non-empty")) "`n"
        . (&Var4 = &NonExistentVar ? "Non-existent" : (Var4 = "" ? "Empty" : "Non-empty")) "`n"
        . (&Var5 = &NonExistentVar ? "Non-existent" : (Var5 = "" ? "Empty" : "Non-empty"))

But really, pretty much always (at least if you design your program well) you'll be fine by just evaluating a boolean value from the variable, as shown in the other answer. This way you can easily just check the variable's existence with in an if-statement if (var).

var1 := ""
var2 := "hello"
var3 := "0"
var4 := false
var5 := -1

MsgBox, % !!var1 "`n"
        . !!var2 "`n"
        . !!var3 "`n"
        . !!var4 "`n"
        . !!var5 "`n"

The only caveat is that there is no difference between false (and 0 (false is a built in variable containing 0)), "" and an actually non-existent variable.


AHKv2 implements its own built-in function for this:
https://lexikos.github.io/v2/docs/commands/IsSet.htm

Upvotes: 1

Spyre
Spyre

Reputation: 2344

Here is the method I typically use to detect empty variables

Code:

;#Warn

^r::
if(!PrevActiveClass){
    MsgBox not set yet
}
else{
    WinActivate ahk_class %PrevActiveClass%
}
return

^e::WinGetClass, PrevActiveClass, A ; Sets the variable to be the active window for testing

In AHK, an empty variable is treated as a boolean false. As such, you can check it in an if statement to determine whether it contains anything. The one caveat with this method (though it does not apply to your use case) is that it does not work as intended if you on assigning the boolean false value to the var.

Upvotes: 0

Related Questions