Page 1 of 2

Using repeat loop to add values

Posted: Wed Oct 25, 2017 5:19 pm
by Josh1910
I feel like I knew this at one point, but I've lost it. Long story short, I wanted to add values in an array together, so something like this:

repeat with z=1 to thisNum
add Numb&z tempSum
end repeat

The problem is that it tries to add "Numb1" instead of the value in the container Numb1. I did it a dreadful way as follows (only because I'm pressed for time):

repeat with z=1 to thisNum
if z =1 then add Numb1 to tempSum
if z =2 then add Numb2 to tempSum
if z =3 then add Numb3 to tempSum
if z =4 then add Numb4 to tempSum
if z =5 then add Numb5 to tempSum
if z =6 then add Numb6 to tempSum
if z =7 then add Numb7 to tempSum
if z =8 then add Numb8 to tempSum
if z =9 then add Numb9 to tempSum
end repeat

But I know that's an inefficient approach. What is the solution?

Would it be:

repeat with z=1 to thisNum
add Numb[z] to tempSum
end repeat

Any help would be appreciated in my future programming. Thanks!

Re: Using repeat loop to add values

Posted: Wed Oct 25, 2017 5:28 pm
by Klaus
Hi Josh,

this is a job for DO! :D

Code: Select all

...
repeat with z = 1 to thisNum
   do("add Numb" & z && "to tempSum")
end repeat
...
However for these kind of situations you should consider to use a numbered ARRAY
instead of lots of single variables, where you can loop through the keys easily!

Best

Klaus

Re: Using repeat loop to add values

Posted: Wed Oct 25, 2017 5:30 pm
by bogs
I thought for adding arrays, it would be more like (not tested)

Code: Select all

repeat for each element x in [arrayName]
   add x to tempSum
end repeat
but I could be off. And Klaus posted as I was heh.

Re: Using repeat loop to add values

Posted: Wed Oct 25, 2017 6:00 pm
by dunbarx
Hi.

You do not really have an array. I think. This is a very special LC variable, and we can discuss it later. In the meantime, consider this in a button script:

Code: Select all

on mouseUp
   put 1 into numb1
   put 2 into numb2
   put 3 into numb3
   put 4 into numb4
   
   repeat with z = 1 to 4
      do "get numb" & z
      add it to tempSum
   end repeat
   answer tempSum
end mouseUp
The ability to evaluate an expression to two levels is required for the sort of construction you created. The "do" command evaluates the expression and then "executes" it. In that evaluation, the concatenation of the string "numb" with the variable "z' takes place, so that the final "add" line makes sense to LC.

There are other ways to do what you are working with, but we can discuss that later as well.

Craig Newman

Re: Using repeat loop to add values

Posted: Wed Oct 25, 2017 6:08 pm
by dunbarx
Hi.

Klaus' construction is the same as mine, but more compact. Do you see how both versions take a string and "evaluate" it twice?

For Klaus, the parenthesis comprises a level of evaluation, and the "do" does it yet again. In mine, perhaps more readable :wink: , each line evaluates on its own.

It is not obvious why LC (and HC before it, where this all first came up) cannot "read" the statement:

Code: Select all

add "numb" & z to tempSum
or even:
add ("numb" & z) to tempSum
in one pass. but it cannot.

Craig

Re: Using repeat loop to add values

Posted: Wed Oct 25, 2017 10:36 pm
by [-hh]
Yet another option: Use sum().

(a) if each of the 9 numX is a number:

Code: Select all

put sum(num1,num2,num3,num4,num5,num6,num7,num8,num9) into tempsum
(b) if numArray is an array containing only numbers (no matter the keys):

Code: Select all

put sum(numArray) into tempsum

Re: Using repeat loop to add values

Posted: Wed Oct 25, 2017 11:25 pm
by SparkOut
The cat is losing skin alarmingly!
Another approach:

Code: Select all

   repeat with i = 1 to thisNum
      add value("Numb" & i) to tTempSum
   end repeat

Re: Using repeat loop to add values

Posted: Thu Oct 26, 2017 12:22 am
by FourthWorld
Josh1910 wrote:
Wed Oct 25, 2017 5:19 pm
But I know that's an inefficient approach. What is the solution?

Would it be:

Code: Select all

     repeat with z=1 to thisNum
         add Numb[z] to tempSum
      end repeat
Yes.

Or Bogs' variant above which uses "repeat for each element...", which will execute slightly faster.

Re: Using repeat loop to add values

Posted: Thu Oct 26, 2017 5:01 am
by bogs
FourthWorld wrote:
Thu Oct 26, 2017 12:22 am
which will execute slightly faster.
Heh, I wonder how many iterations you'd have to have to notice it though. I would guess several hundred?

Re: Using repeat loop to add values

Posted: Thu Oct 26, 2017 7:29 am
by FourthWorld
bogs wrote:
Thu Oct 26, 2017 5:01 am
FourthWorld wrote:
Thu Oct 26, 2017 12:22 am
which will execute slightly faster.
Heh, I wonder how many iterations you'd have to have to notice it though. I would guess several hundred?
Given how fast arrays are in general and the relatively small savings by using the "repeat for each element" option, depending on the size of the array (particularly the number of keys) you'd need at least many thousands of iterations to become measurable, and at least a hundred thousand or more to become noticeable.

Re: Using repeat loop to add values

Posted: Thu Oct 26, 2017 7:49 am
by bogs
Nice, but WOW was I off :mrgreen:

Re: Using repeat loop to add values

Posted: Thu Oct 26, 2017 3:02 pm
by Mikey
SparkOut wrote:
Wed Oct 25, 2017 11:25 pm
The cat is losing skin alarmingly!
Another approach:

Code: Select all

   repeat with i = 1 to thisNum
      add value("Numb" & i) to tTempSum
   end repeat
DANG IT! That was my first idea.

Re: Using repeat loop to add values

Posted: Thu Oct 26, 2017 3:27 pm
by bogs
Don't worry Mikey, your not out yet, we're giving you a second chance !
Image
Start skinning !

Re: Using repeat loop to add values

Posted: Thu Oct 26, 2017 8:51 pm
by [-hh]
I really wonder why you are ignoring the wonderful super fast function sum()?

Instead of "add Numb[z] to tempSum", no matter whether using "repeat with" or "repeat for each",
if Numb is an array containing only numbers then the one-liner

Code: Select all

 put sum(Numb) into tempsum 
is, TMHO, "the" solution, directly given by the engine. No skinning needed, I like cats.

Re: Using repeat loop to add values

Posted: Thu Oct 26, 2017 9:01 pm
by Mikey
Mmmmmmmm Kitty kabobs. Yum.