Page 1 of 1
variable container names
Posted: Fri Dec 31, 2010 7:17 pm
by kwinkler
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.
Re: variable container names
Posted: Fri Dec 31, 2010 7:34 pm
by Dixie
Hi...
look in the 'User guide' page 154... section 5.5.7... all will be made clear
be well
Dixie
Re: variable container names
Posted: Fri Dec 31, 2010 7:49 pm
by kwinkler
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
Re: variable container names
Posted: Fri Dec 31, 2010 8:00 pm
by Dixie
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
Re: variable container names
Posted: Fri Dec 31, 2010 8:08 pm
by kwinkler
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
Re: variable container names
Posted: Fri Dec 31, 2010 8:15 pm
by jmburnod
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
Re: variable container names
Posted: Fri Dec 31, 2010 9:11 pm
by dunbarx
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.
Re: variable container names
Posted: Fri Dec 31, 2010 9:31 pm
by kwinkler
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.
Re: variable container names
Posted: Fri Dec 31, 2010 11:03 pm
by FourthWorld
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]
Re: variable container names
Posted: Tue Jan 04, 2011 8:45 pm
by jsburnett
Not a sophisticated answer:
repeat with k = 1 to 2
put “theData” & k into theData
put xhi after theData
end repeat