create and work with multiple global variables

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

create and work with multiple global variables

Post by jmburnod » Wed Jun 10, 2009 9:52 am

Hi All,

I done 3 scripts for create and work with multiple global variables

On for create each
One for Change the content of each
One for return the content of each

Maeby someone have a best way.
Maeby with array i don't know really at the moment.

I believe i make progress in english Isn't it ? :D

Regards

Jean-Marc

Code: Select all

--•• Create a list of global variables
on dBuildSomeGlobalVar pNomGlob,pPrefixGlob --•• pNomGlob = a list of names of globals. One items for one global
   -- put pNomGlob into LesNomG
   put the num of items of pNomGlob into nb
   repeat with i = 1 to nb
      put item i of pNomGlob into LaL
      put empty into fld Lal
      put pPrefixGlob & Lal into UneG
      put "Global"&&UneG into LeMes
      do LeMes
      put "put empty "&&"into"&& uneG into LeMes
      do LeMes
   end repeat
end dBuildSomeGlobalVar

--•• Change the content of each global variables
on dChangeSomeGlobalVar pNomGlob,pContent,pPrefixGlob --•• pContent = a list of contents for each global. One items for one content for one global
 put the num of items of pNomGlob into nb
   repeat with i = 1 to nb
      put item i of pNomGlob into LaL
      put pPrefixGlob & LaL into UneG
      put "Global"&&UneG into LeMes
      do LeMes
      put "put item i of pContent "&&"into"&& uneG into LeMes
      do LeMes
   end repeat
end dChangeSomeGlobalVar

--•• return the content of each global variables
on dShowContentSomeGlobalVar pNomGlob,pPrefixGlob
   put empty into buf
   put empty into bufDef 
   put the num of items of pNomGlob into nb
   repeat with i = 1 to nb
      put item i of pNomGlob into LaL
      put pPrefixGlob & LaL into UneG
      put pPrefixGlob & LaL into uneVar
      put "Global"&&UneG into LeMes
      do LeMes
      -- put "put"&&UneG&&"into fld"&&LaL into LeMes --••  one global into one fld
      put "put"&&UneG&&"into"&&"buf" into LeMes --•• one global by line into a variable
      do Lemes 
      put uneVar&&"="&&buf&return after bufDef
   end repeat
   dansmes bufDef --•• the result in the messagebox
end dShowContentSomeGlobalVar

on Dansmes t --•• a result in the messagebox
   put t
end Dansmes
Last edited by jmburnod on Sat Jun 13, 2009 8:14 am, edited 2 times in total.

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

Post by FourthWorld » Wed Jun 10, 2009 4:00 pm

You might consider using an array instead, which would let you use a single global to store your data with any number of elements addressable by name:

Code: Select all

on PutGlobal pLabel, pValue
  global gGlobalValuesA
  put pValue into gGlobalValuesA[pLabel]
end PutGlobal

on GetGlobal pLabel
  global gGlobalValuesA
  return gGlobalValuesA[pLabel]
end GetGlobal
Not only is this simpler, but I think you'll find it much faster than anything using "do".
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

create and work with multiple global variables

Post by jmburnod » Wed Jun 10, 2009 11:20 pm

Thank You Richard,

Your post help me to be more friendly with array concept.

I discover a new univers very useful for my work

Best regards

Jean-Marc

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

create and work with multiple global variables

Post by jmburnod » Thu Jun 11, 2009 9:53 pm

I tested a process by array. It work fine and fast

Code: Select all

--•• create the array
on createGlobal pLesLabels
   global gGlobalValuesA
     put the num of items of pLesLabels into nb
      repeat with i = 1 to nb
         put item i of pLesLabels into UnLabel
         PutGlobal UnLabel,""
   end repeat
end createGlobal

--•• Change one line of one element of array
on SetIOneLineOfGlobal plabel,pLine,pNewContent
   put gGlobalValuesA[pLabel] into bufLeLabel
   put pNewContent into line pLine of bufLeLabel
   PutGlobal plabel,bufLeLabel
end SetIOneLineOfGlobal

--•• Change one element of array
on PutGlobal pLabel, pValue
  global gGlobalValuesA
  put pValue into gGlobalValuesA[pLabel]
end PutGlobal
All the best

Jean-Marc

Post Reply