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

REBOL 3 Concepts: Expressions: Selection

Pending Revision

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

There are several methods to selectively evaluate expressions in REBOL. These methods provide a way for evaluation to branch many different ways, based on a key value.



The select function is often used to obtain a particular value or block, given a target value. If you define a block of values and actions, you can use select to search for the action that corresponds to a value.

cases: [
    center [print "center"]
    right  [print "right"]
    left   [print "left"]
action: select cases 'right
if action [do action]

In the previous example, the select function finds the word right and returns the block that follows it. (If for some reason the block was not found, then none! would have been returned.) The block is then evaluated. The values used in the example are words, but they can be any kind of value:

cases: [
     5:00 [print "everywhere"]
    10:30 [print "here"]
    18:45 [print "there"]
action: select cases 10:30
if action [do action]


The select function is used so often that there is a special version of it called switch, which includes the evaluation of the resulting block. The switch function makes it easier to perform inline selective evaluation. For instance, to switch on a simple numeric case:

switch 22 [
    11 [print "here"]
    22 [print "there"]

The switch function also returns the value of the block it evaluates, so the previous example can also be written as:

str: copy "right "

print switch 22 [
    11 [join str "here"]
    22 [join str "there"]
right there


car: pick [Ford Chevy Dodge] random 3
print switch car [
    Ford  [351 * 1.4]
    Chevy [454 * 5.3]
    Dodge [154 * 3.5]

The cases can be any valid datatype, including numbers, strings, words, dates, times, urls, and files. Here are some examples:


person: "kid"
switch person [
    "dad" [print "here"]
    "mom" [print "there"]
    "kid" [print "everywhere"]


person: 'kid
switch person [
    dad [print "here"]
    mom [print "there"]
    kid [print "everywhere"]


person: 123
switch type?/word [
    string! [print "a string"]
    binary! [print "a binary"]
    integer! [print "an integer number"]
    decimal! [print "a decimal number"]
an integer number


file: %rebol.r
switch file [
    %user.r [print "here"]
    %rebol.r [print "everywhere"]
    %file.r [print "there"]


switch url [ [print "here"] [print "there"] [print "everywhere"]


tag: <LI>
print switch tag [
    <PRE>   ["Preformatted text"]
    <TITLE> ["Page title"]
    <LI>    ["Bulleted list item"]
Bulleted list item


time: 12:30
switch time [
     8:00 [send wendy@domain.dom "Hey, get up"]
    12:30 [send cindy@rebol.dom "Join me for lunch."]
    16:00 [send group@every.dom "Dinner anyone?"]

Default Case

A default case can be specified when none of the other cases match. Use the default refinement to specify a default:.

time: 7:00
switch/default time [
     5:00 [print "everywhere"]
    10:30 [print "here"]
    18:45 [print "there"]
] [print "nowhere"]

Common Cases

If you have common cases, where the result would be the same for several values, you can define a word to hold a common block of code:

case1: [print length? url]   ; the common block

switch url [ case1 [print "there"] case1

Other Cases

More than just blocks can be evaluated for cases. This example evaluates the file that corresponds to a day of the week:

switch now/weekday [
    1 %monday.r
    5 %friday.r
    6 %saturday.r

So, if it's Friday, the friday.r file is evaluated and its result is returned from the switch. This type of evaluation also works for URLs:

switch time [

The cases for switch are enclosed in a block, and therefore can be defined apart from the switch statement:

schedule: [
     8:00 [send wendy@domain.dom "Hey, get up"]
    12:30 [send cindy@dom.dom "Join me for lunch."]
    16:00 [send group@every.dom "Dinner anyone?"]

switch 8:00 schedule

  TOC < Back Next > - WIP Wiki Feedback Admin