Monte Goulding wrote:
On 11 Aug 2017, at 7:02 am, Richard Gaskin via use-livecode <use-livecode at
lists.runrev.com> wrote:
Except that's wrong. :)
I tend to use 0 as the initial value, and increment at the top of the loop:
local tIndex
put 0 into tIndex
repeat for each line tLine in tLines
add 1 to tIndex
— code
end repeat
I've found two benefits from this habit:
1. (minor) Clearer distinction between unincremented and incremented counter.
2. (major) I can "next repeat" at any point in the block and know that the
counter is appropriately updated.
Indeed, it was a runaway loop many years ago that set me know this path.
Aha… well it probably depends on what you are using tIndex for but yes your
pattern is much more reliable if tIndex is meant to represent the line number.
If on the other hand tIndex is meant to only be incremented if you haven’t
exited the loop at some point while processing tLine then you may want the
other pattern so perhaps both have value as examples but I’m not sure how one
would name the two different patterns. I guess you could do this which is
probably best:
local tIndex
put 0 into tIndex
repeat for each line tLine in tLines
— code for ensuring tIndex should be incremented
add 1 to tIndex
— code for using tIndex
end repeat
Exactly. Many possible waya to write code. No single template will
capture all needs.
Most of my loops never use counters, or I use the built-in counter form.
"repeat with...".
So many different ways to do things. That's why we use a scripting
language rather than a point-and-click tool. for the flexible
expressiveness.
Sparse minimums are fine. I just need a few examples for the moment.
Let's start with just two:
repeat with i = <startval> to <endVal>
--
end repeat
repeat for each <chunkType> in <container>
--
end repeat
Leave any specific extras for the scripter. We need to be in control o
those things, and many times we don't use them at all. These barebones
are all we need. If we need another line we'll write it.
PS: A nagging thought just occurred to me: if the counter variable is
named in the script, it would need to account for nested loops.
And FWIW I only sometimes use K&R's "i", "j", "k" pattern. Sometimes
I'll use more descriptive names, esp. if the block inside the loop is long.
--
Richard Gaskin
Fourth World Systems
Software Design and Development for the Desktop, Mobile, and the Web
____________________________________________________________________
ambassa...@fourthworld.com http://www.FourthWorld.com
_______________________________________________
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode