Using repeat loop to add values

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

Josh1910
Posts: 19
Joined: Tue Jan 20, 2015 7:30 am

Using repeat loop to add values

Post by Josh1910 » Wed Oct 25, 2017 5:19 pm

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!

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Using repeat loop to add values

Post by Klaus » Wed Oct 25, 2017 5:28 pm

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

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Using repeat loop to add values

Post by bogs » Wed Oct 25, 2017 5:30 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Using repeat loop to add values

Post by dunbarx » Wed Oct 25, 2017 6:00 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Using repeat loop to add values

Post by dunbarx » Wed Oct 25, 2017 6:08 pm

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

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Using repeat loop to add values

Post by [-hh] » Wed Oct 25, 2017 10:36 pm

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
shiftLock happens

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

Re: Using repeat loop to add values

Post by SparkOut » 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

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

Re: Using repeat loop to add values

Post by FourthWorld » Thu Oct 26, 2017 12:22 am

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Using repeat loop to add values

Post by bogs » 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?
Image

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

Re: Using repeat loop to add values

Post by FourthWorld » Thu Oct 26, 2017 7:29 am

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Using repeat loop to add values

Post by bogs » Thu Oct 26, 2017 7:49 am

Nice, but WOW was I off :mrgreen:
Image

Mikey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 755
Joined: Fri Jun 27, 2008 9:00 pm

Re: Using repeat loop to add values

Post by Mikey » Thu Oct 26, 2017 3:02 pm

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.

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Using repeat loop to add values

Post by bogs » Thu Oct 26, 2017 3:27 pm

Don't worry Mikey, your not out yet, we're giving you a second chance !
Image
Start skinning !
Image

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Using repeat loop to add values

Post by [-hh] » Thu Oct 26, 2017 8:51 pm

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.
shiftLock happens

Mikey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 755
Joined: Fri Jun 27, 2008 9:00 pm

Re: Using repeat loop to add values

Post by Mikey » Thu Oct 26, 2017 9:01 pm

Mmmmmmmm Kitty kabobs. Yum.

Post Reply