Page 1 of 1

How to empty several global var at once [SOLVED]

Posted: Thu Apr 10, 2014 2:18 pm
by atout66
Hi to all,

When I declare several global variables in a script, how can I empty all of them at once to avoid this code below ?

Code: Select all

global gVar1 , gVar2 , gVar3 , gVar4 , gVar5 , gVar6
put empty into gVar1
put empty into gVar2
...
It' not exactly the same as for fields like I asked before, because I don't think I can put globals vars in a list, right ?

Kind regards, Jean-Paul.

Re: How to empty several global var at once

Posted: Thu Apr 10, 2014 2:35 pm
by FourthWorld
If you have a really large number of globals, sometimes it can be helpful to consider using an array for those, e.g.:

Code: Select all

global gMyGlobalsA
on mouseUp
   put "Something" into gMyGlobalsA[1]
   put "SomethingElse" into gMyGoobalsA[2]
end mouseUp

on ClearGlobals
   put empty into gMyGlobalsA
end ClearGlobals
In that example they keys are numeric only because that seems to reflect the naming style in your code. You can also use strings for array element keys:

Code: Select all

put "Something" into gMyGlobalsA["SomeKey"]
And you can also use variables as key names:

Code: Select all

put "SomeKey" into tKeyName
put "Something" into gMyGlobalsA[tKeyName]

Re: How to empty several global var at once

Posted: Thu Apr 10, 2014 2:36 pm
by Thierry
atout66 wrote:Hi to all,
When I declare several global variables in a script, how can I empty all of them at once to avoid this code below ?

Code: Select all

global gVar1 , gVar2 , gVar3 , gVar4 , gVar5 , gVar6
put empty into gVar1
put empty into gVar2
...
It' not exactly the same as for fields like I asked before, because I don't think I can put globals vars in a list, right ?
Bonjour Jean-paul,

Here is a sample code with your gVar1, ..2 example
and another one using an array:

Code: Select all

global gVar1, gVar2
global gJP
on mouseUp
   put 33 into gVar1
   put 42 into gVar2
   put 33 into gJP[1]
   put 42 into gJP[2]
   repeat with i=1 to 2
      do "put empty into gVar" & i
      put empty into gJP[ i ]
   end repeat
end mouseUp
Regards,
Thierry

Re: How to empty several global var at once

Posted: Thu Apr 10, 2014 3:17 pm
by atout66
Thanks to all of you :wink:
I didn't know I could concatenate globals var so easily !
BTW, the use of an array seems to me very clever. I'm going to use that approach (and recode many lines !!!!)

Kind regards, Jean-Paul.