variable container names

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
kwinkler
Posts: 61
Joined: Fri Nov 26, 2010 5:59 am

variable container names

Post by kwinkler » Fri Dec 31, 2010 7:17 pm

This should be simple, but I can't figure it out.

I would like to insert data into a series of variables inside a repeat loop. This code will not compile.

repeat with k = 1 to 2
put xhi after ("theData" & k)
end repeat

It gives the error - 'compilation error at line 511 (Chunk: bad destination) near "(", char 15'

But this code works fine.

put xhi after theData1
put xhi after theData2

Is there any way to make the repeat loop work correctly? Thanks in advance.

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: variable container names

Post by Dixie » Fri Dec 31, 2010 7:34 pm

Hi...

look in the 'User guide' page 154... section 5.5.7... all will be made clear

be well

Dixie

kwinkler
Posts: 61
Joined: Fri Nov 26, 2010 5:59 am

Re: variable container names

Post by kwinkler » Fri Dec 31, 2010 7:49 pm

Dixie,

Thanks for the suggestion, but I don't see anything in section 5.5.7 that helps me. What is the trick?

Thanks,

Ken

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: variable container names

Post by Dixie » Fri Dec 31, 2010 8:00 pm

hi...

If you are trying to make an array use [ ]...

Code: Select all

on mouseUp
   repeat with k = 1 to 30
      /* whatever value you choose to give xhi */
      put xhi into theData[k]
   end repeat
   
   repeat with count = 1 to 30
      put theData[count] into line count of fld 1
   end repeat
end mouseUp
This will list xhi 30 times into each line of fld 1... as I don't know what this variable contains...

be well,

Dixie

kwinkler
Posts: 61
Joined: Fri Nov 26, 2010 5:59 am

Re: variable container names

Post by kwinkler » Fri Dec 31, 2010 8:08 pm

Dixie,

The names of the variables are 'theData1', 'theData2', etc. The '1' and '2' are not keys. I am trying to add new elements to the end of arrays named 'theData1', 'theData2', etc.

As I said, these lines work correctly-

put xhi after theData1
put xhi after theData2

Any ideas would be appreciated,

Ken

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

Re: variable container names

Post by jmburnod » Fri Dec 31, 2010 8:15 pm

Hi Ken,

Maybe this script is useful for you

Code: Select all

on testSetGlob pText
   global thedata1,thedata2
   if pText = empty then
      put "xhi" into pText
   end if
   repeat with k = 1 to 2
      put pText&k into buf
      put "theData" & k into NomG
      do "put empty into"&& NomG
      do "put buf into"&& NomG
   end repeat
   put thedata1&return&thedata2
end testSetGlob
Jean-Marc
https://alternatic.ch

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: variable container names

Post by dunbarx » Fri Dec 31, 2010 9:11 pm

The construction you propose seems reasonable, but the LC parser, and HC before it, would not quite accept it. A "do" construction allows the parser another level of evaluation, and this is necessary for compilation.

on mouseup
repeat with k = 1 to 2
do "put xhi into" && "theData" & k
end repeat
end mouseup

Seems the same, as if the "do" is redundant. But it is not. This sort of thing pops up all the time. You get a feel for when you need to consider it, and more often, if what seems like perfectly logical code gives this sort of error, then you likely need to go that route after the fact.

kwinkler
Posts: 61
Joined: Fri Nov 26, 2010 5:59 am

Re: variable container names

Post by kwinkler » Fri Dec 31, 2010 9:31 pm

Thank you to everyone who helped with this problem. My original code looked like this, and did NOT work-

repeat with k = 1 to 2
put xhi after ("theData" & k)
end repeat


By using the 'do' command, the code is re-written as follows, and this DOES WORK-

repeat with k = 1 to 2
do "put xhi after " && "theData" & k
end repeat

Now I need to study the 'do' command to find out why this works. I had never heard of this command before.

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

Re: variable container names

Post by FourthWorld » Fri Dec 31, 2010 11:03 pm

You might also consider using an array, in which you could store all your data in one variable with named slots:

put "something" into tData[1]
put "somethingelse" into tData[2]
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

jsburnett
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 121
Joined: Fri Mar 09, 2007 9:47 pm

Re: variable container names

Post by jsburnett » Tue Jan 04, 2011 8:45 pm

Not a sophisticated answer:

repeat with k = 1 to 2
put “theData” & k into theData
put xhi after theData
end repeat

Post Reply