Putting values into globals via a repeat loop

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Putting values into globals via a repeat loop

Post by gyroscope » Wed Oct 14, 2009 7:35 pm

Hi, sorry to cover old ground; I seem to remember this being mentioned a couple of months ago but can't find the post...

I have a set of 48 global variables and a list of up to 48 words.

One would think the following would work (although I understand why it doesn't, if that makes sense):

Code: Select all


global gWordCount01
global gw1,gw2,gw3,gw4,gw5,gw6,gw7,gw8,gw9,gw10,gw11,gw12,gw13,gw14,gw15,gw16,gw17,gw18,gw19,gw20,gw21,gw22,gw23,gw24
global gw25,gw26,gw27,gw28,gw29,gw30,gw31,gw32,gw33,gw34,gw35,gw36,gw37,gw38,gw39,gw40,gw41,gw42,gw43,gw44,gw45,gw46,gw47,gw48

on mouseUp
repeat with tWords= 1 to gWordCount
put tWords into (gw&tWords)
end repeat
end mouseUp
Is there a neat scripting solution to what I'm after, please :?:

:)

Additional question: is there a way to script to make a local variable a global one :?:

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Post by bn » Wed Oct 14, 2009 7:56 pm

Hi Gyroscope,

try:

Code: Select all

on mouseUp
   repeat with tWords= 1 to gWordCount01
      do "put tWords into gw" & tWords & ""
   end repeat 
end mouseUp
of course with all your global variables declared.
Why dont you use an array in a custom property? Seems to be a lot easier.
regards
Bernd

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Post by gyroscope » Wed Oct 14, 2009 9:06 pm

Ah yes, I remember now Bernd, thank you. I'll staple that solution into my mind...
Why dont you use an array in a custom property? Seems to be a lot easier.
I still shy away from custom properties; I'm determined to make it click with me one day.

:)

Mark Smith
Posts: 179
Joined: Sat Apr 08, 2006 11:08 pm
Contact:

Post by Mark Smith » Thu Oct 15, 2009 10:48 pm

You could also have a global array:

Code: Select all

global tw

on mouseUp 
  repeat with tWords = 1 to gWordCount 
    put tWords into tw["gw" & tWords]
  end repeat 
end mouseUp
Best,

Mark Smith

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Post by gyroscope » Sat Oct 17, 2009 10:21 pm

That's useful, thank you Mark.

:)

Post Reply