Page 1 of 1
Dynamic variable in Repeat?
Posted: Mon Mar 10, 2014 3:56 am
by trenatos
I have a stack with 15 fields, and I'm hoping to create a dynamic repeat for doing some basic math.
The code below does not work, but should show what I'm trying to achieve (The fields are named price1 - price15)
Is this possible in LC?
Code: Select all
Repeat with counter = 1 to 15
put field "price" & counter + tempPrice into tempPrice
end Repeat
put tempPrice into field "total"
Re: Dynamic variable in Repeat?
Posted: Mon Mar 10, 2014 4:10 am
by Simon
Not sure but
put field "price" & counter + tempPrice into tempPrice
should be
put field "price" & (counter + tempPrice) into tempPrice
But show me what tempPrice should look like after the process.
Simon
Re: Dynamic variable in Repeat?
Posted: Mon Mar 10, 2014 4:12 am
by trenatos
Hi Simon,
I tried but it breaks with; no such object near "price", char 7
Basically I'm just trying to sum up 15 price-fields and display a total.
Re: Dynamic variable in Repeat?
Posted: Mon Mar 10, 2014 4:23 am
by Simon
Sorry try this
put field ("price" & counter) + tempPrice into tempPrice
Does that do it?
Simon
Re: Dynamic variable in Repeat?
Posted: Mon Mar 10, 2014 4:33 am
by trenatos
Yes! That did the trick, and now I know how to make dynamic variables

Re: Dynamic variable in Repeat?
Posted: Mon Mar 10, 2014 4:40 am
by Simon
Glad it worked.
See the counter and temprice are both numbers so liveCode adds them together first, then was looking for a field called "price1234" (whatever). Putting the parenthesis around price & count makes it perform that action first then any others.
Simon
Re: Dynamic variable in Repeat?
Posted: Tue Jun 03, 2014 3:28 am
by renevanp
How does this work if you want to do this with a variable that is not a field or button?
I have the variables: varname1 varname2 varname3 etc
I want to do something like this :
put "xx" into ("varname") & counter
But this gives me an error : (Chunk: bad destination)
So the question is what is the right way d\to do this
Rene
Re: Dynamic variable in Repeat?
Posted: Tue Jun 03, 2014 4:04 am
by Simon
Hi Rene,
Here it is;
do "put xx into varname" & counter
Now if that "xx" is changing then it gets a bit more complex.
do "put"&& xx &&"into varname" & counter
How's that work for you?
Simon
Edited; added &&
Re: Dynamic variable in Repeat?
Posted: Tue Jun 03, 2014 5:56 am
by FourthWorld
Arrays....
Re: Dynamic variable in Repeat?
Posted: Tue Jun 03, 2014 6:15 pm
by renevanp
Thank you Simon and Richard
The arrays work beter for me.