Multiple variables with same name

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Jos
Posts: 5
Joined: 23 Jul 2019, 17:12

Multiple variables with same name

Post by Jos » 06 Dec 2021, 18:42

Hi,

I want some help.

I am trying to clear a variable, but it is not getting cleared reliably.

When I checked the details of the issue, I found that the roots of the issue is that multiple variables are being created with the same name. From the autohotkey variables log file, I find an entry for the variable like below.
variable_name[1 of 3]

So I believe I am clearing one out of these 3 variables, and I am trying to use another variable.

It doesn't make any difference even if include a global declaration at the beginning. I have not declared this variable with a local scope, but this variable is being used in different functions.

I use the below code for global declaration
global variable_name

For clearing the variable I use

Code: Select all

variable_name := ""
I find that this is working for that instance of the variable, since if I retrieve the value of the variable just after clearing it, it is found to be cleared, but when I try to read it in a different function it's not working, since the previous value present in the variable is displayed.

So, what I want is a single global variable, which retains only single value for the whole scope of the code. I think I cannot use static, since I cannot use it outside a function.
So, I want to know, why and when does autohotkey create multiple variables with same name, and how to avoid it.
Can anyone of you help me with this please.

I haven't shared the code, since it is long and complex.

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Multiple variables with same name

Post by mikeyww » 06 Dec 2021, 19:34

Variables are global by default, except in functions, where they are local by default.

The problem likely relates to your long and complex script-- which probably has a bug if you are seeing something unexpected happening. Shorten your script for testing; you may thereby find the problem.

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Multiple variables with same name

Post by swagfag » 06 Dec 2021, 20:59

[1 of 3] refers to how many characters the variable currently contains and what its total capacity is
ur analysis is incorrect and u most likely have bugs in ur code

Post Reply

Return to “Ask for Help (v1)”