Managing math with many fields

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
vascoribeiro
Posts: 33
Joined: Tue Apr 25, 2017 8:13 pm

Managing math with many fields

Post by vascoribeiro » Sat Sep 29, 2018 4:41 pm

Hello!

I'm sure there must be a simpler way to code the operations I'm trying to do but I didn't discovered it... So, if someone could help or give me ideias would be wonderful.

My intention would be to do something like this:

Code: Select all

  put (field "time1" - field "time2")/2 into tTime1
   put (field "time2" - field "time3")/2 into tTime2
   put (field "time3" - field "time4")/2 into tTime3
   put (field "time4" - field "time5")/2 into tTime4
I think I could use the repeat function but I'm not finding the way how to... Any ideias?

Thank you!

Klaus
Posts: 13820
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Managing math with many fields

Post by Klaus » Sat Sep 29, 2018 5:30 pm

Hola Vasco,

we can only deal with these kind of "numbered" variables with a DO statement, which is clumsy and not as fast as possible without DO!

However we can easily use an ARRAY in situations like these, which has (can have) in fact numbered keys:

Code: Select all

...
repeat with i = 1 to 4
   put (fld ("time" & i) - fld ("time" & i + 1))/2 into tTimeArray[i]
end repeat
...
In this/your example you will end with an array with four keys:
tTimeArray[1]
tTimeArray[2]
tTimeArray[3]
tTimeArray[4]
instead of four variables.

Best

Klaus

vascoribeiro
Posts: 33
Joined: Tue Apr 25, 2017 8:13 pm

Re: Managing math with many fields

Post by vascoribeiro » Sat Sep 29, 2018 5:34 pm

Amazing Klaus!

Thank you so much for your feedback. I'll give feedback latter on as usual.

:D

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9647
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Managing math with many fields

Post by dunbarx » Sun Sep 30, 2018 2:37 am

Hey, some of my best friends are old-fashioned.

Vasco, just so you have a sense of how we had to do it in the old days, please take a look at this:

Code: Select all

on mouseUp
   repeat with y = 1 to 4
      do " put (y * 10)  into" &&  tTime & y
      breakpoint  -- to see each iteration
      end repeat
end mouseUp
The "do" construction creates the incrementing variable on the fly. "Do" also can force an additional level of evaluation, which is often the best way out of a situation that seems like it ought to work, but does not. You will know this sort of thing when you come across it.

Anyway, my point is that "do" still has value, and the above example might teach you something about how the program flow in LiveCode works, even if it is old-fashioned.

Craig Newman

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”