Page 1 of 1

Managing math with many fields

Posted: Sat Sep 29, 2018 4:41 pm
by vascoribeiro
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!

Re: Managing math with many fields

Posted: Sat Sep 29, 2018 5:30 pm
by Klaus
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

Re: Managing math with many fields

Posted: Sat Sep 29, 2018 5:34 pm
by vascoribeiro
Amazing Klaus!

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

:D

Re: Managing math with many fields

Posted: Sun Sep 30, 2018 2:37 am
by dunbarx
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