How to empty several global var at once [SOLVED]

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
atout66
Posts: 266
Joined: Wed Feb 02, 2011 12:31 pm

How to empty several global var at once [SOLVED]

Post by atout66 » Thu Apr 10, 2014 2:18 pm

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.
Last edited by atout66 on Thu Apr 10, 2014 3:18 pm, edited 1 time in total.
Discovering LiveCode Community 6.5.2.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: How to empty several global var at once

Post by FourthWorld » Thu Apr 10, 2014 2:35 pm

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]
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: How to empty several global var at once

Post by Thierry » Thu Apr 10, 2014 2:36 pm

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
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

atout66
Posts: 266
Joined: Wed Feb 02, 2011 12:31 pm

Re: How to empty several global var at once

Post by atout66 » Thu Apr 10, 2014 3:17 pm

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.
Discovering LiveCode Community 6.5.2.

Post Reply