Using repeat loop to add values
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Using repeat loop to add values
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!
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
Hi Josh,
this is a job for DO!
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
this is a job for DO!

Code: Select all
...
repeat with z = 1 to thisNum
do("add Numb" & z && "to tempSum")
end repeat
...
instead of lots of single variables, where you can loop through the keys easily!
Best
Klaus
Re: Using repeat loop to add values
I thought for adding arrays, it would be more like (not tested)
but I could be off. And Klaus posted as I was heh.
Code: Select all
repeat for each element x in [arrayName]
add x to tempSum
end repeat

Re: Using repeat loop to add values
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:
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
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
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
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
, 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:
in one pass. but it cannot.
Craig
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

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
Craig
Re: Using repeat loop to add values
Yet another option: Use sum().
(a) if each of the 9 numX is a number:
(b) if numArray is an array containing only numbers (no matter the keys):
(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
Code: Select all
put sum(numArray) into tempsum
shiftLock happens
Re: Using repeat loop to add values
The cat is losing skin alarmingly!
Another approach:
Another approach:
Code: Select all
repeat with i = 1 to thisNum
add value("Numb" & i) to tTempSum
end repeat
-
- VIP Livecode Opensource Backer
- Posts: 10043
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Using repeat loop to add values
Yes.Josh1910 wrote: ↑Wed Oct 25, 2017 5:19 pmBut 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
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Using repeat loop to add values
Heh, I wonder how many iterations you'd have to have to notice it though. I would guess several hundred?

-
- VIP Livecode Opensource Backer
- Posts: 10043
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Using repeat loop to add values
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Using repeat loop to add values
DANG IT! That was my first idea.SparkOut wrote: ↑Wed Oct 25, 2017 11:25 pmThe 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
Don't worry Mikey, your not out yet, we're giving you a second chance !

Start skinning !

Start skinning !

Re: Using repeat loop to add values
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
is, TMHO, "the" solution, directly given by the engine. No skinning needed, I like cats.
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
shiftLock happens
Re: Using repeat loop to add values
Mmmmmmmm Kitty kabobs. Yum.