fun with for loops
[1/9] from: chalz:earthlink at: 1-Aug-2002 22:24
Howdy folks. Got a bit of a difficulty here. Say I'm evaluating a for
loop, like reading lines of text from a file. At the beginning of my for loop,
I have it check to see if the line is commented, blank, or neither. If it's
commented or blank, I want it to just skip to the next iteration of the for
loop; however, if it's neither, let it keep going. I would prefer to do this
with a simple if [] as opposed to a heavy either [][]... 'break' doesn't do
what I need - it kills the for loop entirely, instead of skipping to the next
iteration. If I use either [][], I have to do:
either condition? [
if true do this
][
if false
do these
next 20 lines
all indented
with more indentation
yet to come
very long lines
]
You follow me? It's a coding habit I've picked up from another language.
Thanks folks.
--Charles
[2/9] from: anton:lexicon at: 2-Aug-2002 15:09
A trick I've used for "continue" is
to break out of a loop, like this:
for ... [
loop [
if condition [break] ; simulate "continue"
; other code
]
]
Anton.
[3/9] from: anton:lexicon at: 2-Aug-2002 15:11
Whoops, I missed an argument to 'loop:
for ... [
loop 1 [ ; only once
if condition [break]
; other code
]
]
Anton.
[4/9] from: carl:cybercraft at: 2-Aug-2002 18:22
On 02-Aug-02, Charles wrote:
> Howdy folks. Got a bit of a difficulty here. Say I'm evaluating a
> for loop, like reading lines of text from a file. At the beginning
<<quoted lines omitted: 18>>
> You follow me? It's a coding habit I've picked up from another
> language. Thanks folks.
I'm not sure I do follow you, but if you just want to use an if
instead of an either, just use 'not to switch the result of your
condition around. ie...
parse-line: func [file][
foreach line file [
if not any [line = "" line/1 = #";"][print line]
]
]
>> a-file: ["aaa" "" "bbb" "; a comment" "ccc"]
== ["aaa" "" "bbb" "; a comment" "ccc"]
>> parse-line a-file
aaa
bbb
ccc
HTH.
--
Carl Read
[5/9] from: anton:reisacher:uniklinikum-giessen at: 2-Aug-2002 9:27
How about this
while [
it is true
] [
catch [
if condition1 [
do something
throw
]
if condition2 [
do it too
throw
]
do something else
]
]
or just use switch as it is mostly suitable.
AR
Anton wrote:
[6/9] from: g:santilli:tiscalinet:it at: 2-Aug-2002 11:28
Hi Charles,
On Friday, August 02, 2002, 4:24:21 AM, you wrote:
C> either condition? [
C> if true do this
C> ][
C> if false
C> do these
C> next 20 lines
C> all indented
C> with more indentation
C> yet to come
C> very long lines
C> ]
Is really indentation such a big problem?
Anyway, I think you could do something like:
processing-func: func [etc.] [
if condition? [exit]
do-all-your-work
]
for etc. [
processing-func args
]
or maybe use something like:
for etc. [catch[
if condition? [throw 'get-me-out-of-here]
do-all-your-work
]]
Regards,
Gabriele.
--
Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer
Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r
[7/9] from: greggirwin:mindspring at: 2-Aug-2002 10:26
Hi Charles,
Using ";" as an example for comment lines, but writing a COMMENT? function
might be a good idea.
>> b: ["" "data" ";Comment" "data 2" "" "data 3"]
== ["" "data" ";Comment" "data 2" "" "data 3"]
>> foreach item b [
[ if all [(not empty? item) (#";" <> item/1)][
[ print item
[ ]
[ ]
data
data 2
data 3
--Gregg
[8/9] from: lmecir:mbox:vol:cz at: 2-Aug-2002 23:04
Hi Charles,
if I understood correctly, you would like to have:
set-words: function [
{Get all set-words from a block}
block [block!]
] [elem words] [
words: make block! length? block
parse block [
any [
set elem set-word! (
insert tail words to word! :elem
) | skip
]
]
words
]
cfor: function [
{a C-like for function}
[throw]
init [block!]
test [block!]
inc [block!]
body [block!]
] [use-words cont] [
use-words: append set-words init [continue]
body: append reduce [:catch body] inc
cont: reduce [does [throw none]]
use use-words reduce [
:set [continue] cont
:do init
:while test body
]
]
Usage:
cfor [i: 1] [i <= 3] [i: i + 1] [
if i = 2 [continue]
print i
]
1
3
-L
----- Original Message -----
From: "Charles"
Howdy folks. Got a bit of a difficulty here. Say I'm evaluating a for
loop, like reading lines of text from a file. At the beginning of my for
loop,
I have it check to see if the line is commented, blank, or neither. If it's
commented or blank, I want it to just skip to the next iteration of the for
loop; however, if it's neither, let it keep going. I would prefer to do
this
with a simple if [] as opposed to a heavy either [][]... 'break' doesn't do
what I need - it kills the for loop entirely, instead of skipping to the
next
iteration. If I use either [][], I have to do:
either condition? [
if true do this
][
if false
do these
next 20 lines
all indented
with more indentation
yet to come
very long lines
]
You follow me? It's a coding habit I've picked up from another language.
Thanks folks.
--Charles
[9/9] from: chalz:earthlink at: 2-Aug-2002 12:58
> Is really indentation such a big problem?
Yeah, for me, anyways. Further down, I'm nested some 6 times, and that's
leaving a ton of white space and extraneous scrolling past the edge. For
readability, I always indent whenever a new block is opened.
> Anyway, I think you could do something like:
>
<snip>
> or maybe use something like:
>
<snip>
Thanks, for the thousandth time, to everyone who's responded. I will be
taking all of your suggestions into consideration.
Er, that sounds like the response to a job application. What I mean is,
I've spent the past, hm, 6 years working in this one language which is rather
C-like, but it's not C - it's an interpreted language in a specific development
environment. In it, I could do things like:
<pseudocode>
for line in text {
if blank line {
continue;
// makes the for loop go to the next line
}
everything else
}
So, I could use 'break' to cancel the for entirely, or use 'continue' to
just skip on by. I have come into the habit of frequently using if blocks at
the beginning of functions to test various conditions for which other blocks of
code should be ignored. Ie, if the line is blank, skip it all. Further down,
it checks if the line is commented; skip everything else, then. Otherwise, do
everything else.
I think that Gabriele's idea of setting up another function is what I will
most likely have to do. Oh well - that main function was getting awful hefty
anyways ;) Thanks folks.
--Charles
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted