Dynamic variable in Repeat?

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
trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Contact:

Dynamic variable in Repeat?

Post by trenatos » Mon Mar 10, 2014 3:56 am

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"
Marcus

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Dynamic variable in Repeat?

Post by Simon » Mon Mar 10, 2014 4:10 am

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Contact:

Re: Dynamic variable in Repeat?

Post by trenatos » Mon Mar 10, 2014 4:12 am

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.
Marcus

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Dynamic variable in Repeat?

Post by Simon » Mon Mar 10, 2014 4:23 am

Sorry try this
put field ("price" & counter) + tempPrice into tempPrice
Does that do it?

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Contact:

Re: Dynamic variable in Repeat?

Post by trenatos » Mon Mar 10, 2014 4:33 am

Yes! That did the trick, and now I know how to make dynamic variables :)
Marcus

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Dynamic variable in Repeat?

Post by Simon » Mon Mar 10, 2014 4:40 am

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

renevanp
Posts: 10
Joined: Mon Jan 12, 2009 8:31 pm

Re: Dynamic variable in Repeat?

Post by renevanp » Tue Jun 03, 2014 3:28 am

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

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Dynamic variable in Repeat?

Post by Simon » Tue Jun 03, 2014 4:04 am

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 &&
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

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

Re: Dynamic variable in Repeat?

Post by FourthWorld » Tue Jun 03, 2014 5:56 am

Arrays....
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

renevanp
Posts: 10
Joined: Mon Jan 12, 2009 8:31 pm

Re: Dynamic variable in Repeat?

Post by renevanp » Tue Jun 03, 2014 6:15 pm

Thank you Simon and Richard
The arrays work beter for me.

Post Reply