Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Please, add a command to clean all variables at once.


  • Please log in to reply
20 replies to this topic
Hugme
  • Guests
  • Last active:
  • Joined: --
In my script is long and it uses many variables. But some of the variables are used in different places of the script.
The problem is that if I could execute the script again (otherwise, vars will have outdated info, and since I have lots of checks like "if var" [check for existence of var], the script works buggy if I run it twice.
So I have to clean variables after each execution.
The list of variables is long, I'm still working on the script, so new vars appear, old ones get renamed and so on.
That's a pain in the ass, to maintain just a list of vars to clean.

It would be mega awesome if there would be a command like
CleanAllVariables
which would do what it says.

Eedis
  • Members
  • 1775 posts
  • Last active: Aug 14 2015 06:33 PM
  • Joined: 12 Jun 2009
You're using a scripting language to automate mundane tasks and you're complaining about having to repeat a mundane task? :p
AutoHotkey state, the forum, Poly, and Drainx1. The short story.
I love my wife, my life, my atomic-match; for giving me the greatest gift a man could ask for, such a perfect and beautiful little girl.
9rjbjc.png

fragman
  • Members
  • 1591 posts
  • Last active: Nov 12 2012 08:51 PM
  • Joined: 13 Oct 2009
There are a few things that can be done for this:

A) Don't rely on the state of a variable. Instead, check for its value or set/clear it where appropriate.
B) Avoid using global variables except when necessary. Instead, use local variables in functions or objects.

  • Guests
  • Last active:
  • Joined: --
This will clear all variables

Replaces the currently running instance of the script with a new one.
Source: http://www.autohotke...ands/Reload.htm


:D

Guest
  • Guests
  • Last active:
  • Joined: --

But some of the variables are used in different places of the script.
the script works buggy if I run it twice.

Replaces the currently running instance of the script with a new one.



MasterFocus
  • Moderators
  • 4323 posts
  • Last active: Jan 28 2016 01:38 AM
  • Joined: 08 Apr 2009
Aren't you using an IDE?

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Antonio Fran├ža -- git.io -- github.com -- ahk4.net -- sites.google.com -- ahkscript.org

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.


guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011

That's a pain in the ass, to maintain just a list of vars to clean.


thats programming. its an art to design code well instead of creating a ton of variables. its hard work :)

G. Sperotto
  • Members
  • 539 posts
  • Last active: Jun 20 2015 04:54 PM
  • Joined: 12 Dec 2011
You could also create a function to clean up all variables. I usually do that. And Function calls are a single-line.

A command like that wouldn't be that usefull because in most cases, you actually wish to preserve "that particular one variable".

CleanAllVariables()
{
 Global
 Var1 := "", Var2 := "", Var3 := "", Var4 := "", Var5 := "", Var6 := "", Var7 := "", Var8 := "" [color=green]; etc[/color]
 Loop, 100
 {
   Variable%A_index% := "" [color=green];  if some routine creates an unknown amount of variables.[/color]
 }
}
return

"What is a suitable automation? Whatever saves your day for the greater matters."
Barcoder - Create QR Codes and other Barcodes using only Autohotkey !!


  • Guests
  • Last active:
  • Joined: --
CleanAllVariables() as shown above won't do very much as the variables aren't global :D

G. Sperotto
  • Members
  • 539 posts
  • Last active: Jun 20 2015 04:54 PM
  • Joined: 12 Dec 2011

CleanAllVariables() as shown above won't do very much as the variables aren't global :D


True.

My fault for not testing the example :oops:

CleanAllVariables()
{
 [color=red]Global[/color]
 Var1 := "", Var2 := "", Var3 := "", Var4 := "", Var5 := "", Var6 := "", Var7 := "", Var8 := "" [color=green]; etc[/color]
 Loop, 100
 {
   Variable%A_index% := "" [color=green];  if some routine creates an unknown amount of variables.[/color]
 }
}
return

Corrected.

"What is a suitable automation? Whatever saves your day for the greater matters."
Barcoder - Create QR Codes and other Barcodes using only Autohotkey !!


sinkfaze
  • Moderators
  • 6367 posts
  • Last active: Nov 30 2018 08:50 PM
  • Joined: 18 Mar 2008
Wouldn't this be just as easy?

CleanGlobalVars() {

	global

	ListVars	[color=#00BF00]; switch AHK window to list all global vars[/color]
	WinHide, ahk_class AutoHotkey	[color=#00BF00]; hide the window[/color]
	ControlGetText, out, Edit1, ahk_class AutoHotkey	[color=#00BF00]; get the list of vars[/color]
	out :=	RegExReplace(out,"`am)\[.*$")	[color=#00BF00]; remove the additional text for each var[/color]
	Loop, parse, out, `n, `r	[color=#00BF00]; parse the list of var names[/color]
		if	A_Index < 3	[color=#00BF00]; skip the first two lines[/color]
			continue
		else	VarSetCapacity(%A_LoopField%,0)	[color=#00BF00]; clean baby, clean[/color]
}


guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011
yeah i was thinking of using the ListVars approach too just didnt know how reliable it would be.

thats probably the best solution

CleanVariables
  • Guests
  • Last active:
  • Joined: --
sinkfaze,

I tried your approach and it does not appear to clear the variables.

I may be misunderstanding the goal, but I thought it was to make the variables blank.

The variables have the same values before and after.

Here is my test code.
VarOne = 123456789
VarTwo = 0005

MsgBox,,Before, VarOne: %VarOne%`n`nVarTwo: %VarTwo%
CleanGlobalVars()
MsgBox,,After, VarOne: %VarOne%`n`nVarTwo: %VarTwo%

return

CleanGlobalVars() {

   global

   ListVars   ; switch AHK window to list all global vars
   WinHide, ahk_class AutoHotkey   ; hide the window
   ControlGetText, out, Edit1, ahk_class AutoHotkey   ; get the list of vars
   out :=   RegExReplace(out,"`am)\[.*$")   ; remove the additional text for each var
   Loop, parse, out, `n, `r   ; parse the list of var names
      if   A_Index < 3   ; skip the first two lines
         continue
      else   VarSetCapacity(%A_LoopField%,0)   ; clean baby, clean
}


sinkfaze
  • Moderators
  • 6367 posts
  • Last active: Nov 30 2018 08:50 PM
  • Joined: 18 Mar 2008
Try this instead (switched a couple of lines):

CleanGlobalVars() {

   global

   ListVars   ; switch AHK window to list all global vars
   ControlGetText, out, Edit1, ahk_class AutoHotkey   ; get the list of vars
   WinHide, ahk_class AutoHotkey   ; hide the window
   out :=   RegExReplace(out,"`am)\[.*$")   ; remove the additional text for each var
   Loop, parse, out, `n, `r   ; parse the list of var names
      if   A_Index < 3   ; skip the first two lines
         continue
      else   VarSetCapacity(%A_LoopField%,0)   ; clean baby, clean
}


CleanVariables
  • Guests
  • Last active:
  • Joined: --
Here is the error I get.

Error: The following variable name contains an illegal character:
"Global Variables (alphabetical)"

Line#
015: ControlGetText,out,Edit1,ahk_class AutoHotkey
016: WinHide,ahk_class AutoHotkey
017: out := RegExReplace(out,"m)\[.*$")
018: Loop,parse,out,
,

019: if A_Index < 3
020: Continue
021: Else
---> 021: VarSetCapacity(%A_LoopField%,0)
022: }
023: Exit
024: Exit
024: Exit

The current thread will exit.