skip a value inside 'foreach
[1/4] from: hallvard::ystad::helpinhand::com at: 16-Nov-2002 15:24
Hello
I'm inside a 'foreeach. When I get to a certain value, say 5, I'd like to _not_ execute
the execution block, but go on to the next value. I cannot accomplish this with 'break,
because I want to continue the 'foreach loop.
In Java, I'd do a continue. How do I do it in Rebol?
Thanks,
~H
[2/4] from: lmecir:mbox:vol:cz at: 16-Nov-2002 16:33
Hi Halvard,
you can use e.g.
cfor [i: 1] [i <= 3] [i: i + 1] [
if i = 2 [continue]
print i
]
-L
The CFOR function is (much) faster, than FOR and here is its implementation:
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
]
]
[3/4] from: anton:lexicon at: 18-Nov-2002 14:05
You could do this:
foreach val [1 2 3 4 5 6][
loop 1 [
if val = 5 [break]
print val
]
]
Anton.
[4/4] from: al:bri:xtra at: 18-Nov-2002 17:27
How about this:
foreach Value [1 2 3 4 5 6 7] [
if 5 <> Value [
print Value
]
]
And running it at the console:
>> foreach Value [1 2 3 4 5 6 7] [
[ if 5 <> Value [
[ print Value
[ ]
[ ]
1
2
3
4
6
7
Andrew Martin
ICQ: 26227169 http://valley.150m.com/