REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 6-Feb-2009 Edit History  

REBOL 3 Concepts: Series: Series Iteration

Pending Revision

This document was written for R2 and has yet to be revised for R3.

You can use a loop to traverse a series. There are a few loop functions that can help automate the iteration process.

Contents

Foreach Loop

The foreach loop moves through a series setting a word or multiple words in to the values in the series.

The foreach loop takes three arguments: a word or a block of words that holds the values for each iteration, a series, and a block to evaluate for each iteration.

colors: [red green blue yellow orange gold]
foreach color colors [print color]
red
green
blue
yellow
orange
gold
foreach [c1 c2] colors [print [c1 c2]]
red green
blue yellow
orange gold
foreach [c1 c2 c3] colors [print [c1 c2 c3]]
red green blue
yellow orange gold

This is very useful with blocks that contain related values:

people: [
    "Bob" bob@example.com 12
    "Tom" tom@example.net 40
    "Sam" sam@example.org 22
]
foreach [name email age] people [
    print [name email age]
]
Bob bob@example.com 12
Tom tom@example.net 40
Sam sam@example.org 22

Note that the foreach loop does not advance the current index through the series, so there is no need to reset its series variable.

While Loop

The most flexible approach is to use a while loop, which allows you to do just about anything to the series without problems.

colors: [red green blue yellow orange]

while [not tail? colors] [
    print first colors
    colors: next colors
]
red
green
blue
yellow
orange

The method shown below allows you to insert values without hitting a value twice:

colors: head colors

while [not tail? colors] [
    if colors/1 = 'yellow [
        colors: insert colors 'blue
    ]
    colors: next colors
]

This example illustrates that the insert returns the position immediately following the insertion.

To remove a value without accidentally skipping a value, use the following code:

colors: head colors

while [not tail? colors] [
    either colors/1 = 'blue [
        remove colors
    ][
        colors: next colors
    ]
]

Notice that if a removal is done, the next function is not performed.

Forall Loop

The forall loop is similar to the while loop, but eliminates some of the effort required. The forall loop starts from the current index and advances through a series to its tail evaluating a block for every value.

The forall loop takes two arguments: a series variable and a block to evaluate for each iteration.

colors: [red green blue yellow orange]

forall colors [print first colors]
red
green
blue
yellow
orange

The forall advances the variable position through the series, so when it returns the variable is left at its tail:

print tail? colors
true

Therefore, the variable must be reset before it is used again:

colors: head colors

Also, if the block modifies the series, be careful to avoid missing or repeating a value. The forall loop works in some cases; however, if you are uncertain, use the while loop instead.

forall colors [
    if colors/1 = 'blue [remove colors]
    print first colors
]
red
green
yellow
orange

Forskip Loop

Similar to forall, the forskip loop advances through a series starting at the current position, but skips the specified number of values each time.

The forskip loop takes three arguments: a series variable, the skip between each iteration, and a block to evaluate for each iteration.

colors: [red green blue yellow orange]

forskip colors 2 [print first colors]
red
blue
orange

The forskip loop leaves the series at its tail, requiring you to reset it.

print tail? colors
true
colors: head colors

The Break Function

Any of the loops can be stopped at any time by evaluating the break function from within the evaluation block. See the [bad-link:concepts/expressions.txt] Chapter for more information about the break function.


  TOC < Back Next > REBOL.com - WIP Wiki Feedback Admin