Circular Series of Values
Author: Patrick Philipot Return to REBOL Cookbook
Here is a simple trick to obtain a repeated set of values from a series,
from the first to the last, then wrapping back to the first again.
For example, if you have the days of the week stored as words in a block:
days: [sunday monday tuesday wednesday thursday friday saturday]
|
The trick is quite simple. The first value is copied to the end of the block:
day: first days
append days day
|
Now you can call SELECT to get each of the next day values in order
(without end):
day: 'sunday
repeat i 31 [
print day: select days day
]
|
Why it Works |
The SELECT function searches for a value, then returns the value
that follows it. Putting the first value also at the end lets
the SELECT function work for the saturday value, by finding sunday
after it.
|
|