# switch

Another flow control command

Usage

    switch [-p] (<expression>)
    {
        case(<value>)[:]<command>
        [break]
        case(<value>)[:]<command>
        [break]
        ....
        match(<wildcard_expression>)[:]<command>
        [break]
        ....
        regexp(<regular_expression>)[:]<command>
        [break]
        ....
        case(<value>)[:]<command>
        [break]
        ....
        default[:]<command>
        [break]
    }

Description

The switch command is based on the standard C 'switch' keyword. It conditionally executes groups of commands chosen from a larger set of command groups.
First <expression> is evaluated (<expression> is any arithmetic or string expression).
Then the 'match', 'regexp', 'case' and 'default' labels are evaluated sequentially in the order of appearance.
case(<value>)[:]<command>
The <value> is evaluated and is compared against the result of <expression>. String comparison is case insensitive.
If <value> is equal to <expression> then <command> is executed. Please note that <command> must be either a single instruction or an instruction block enclosed in braces. During or after <command> execution, if a break statement is encountered the execution of the switch is terminated, otherwise the next label is evaluated.
If the -p (--passthrough) option is enabled, than the switch command will execute all the istructions blocks until a break statement is found.
match(<value>)[:]<command>
The <value> is expected to be a wildcard expression (wildcard characters being '*' and '?') that is matched in a case insensitive fashion against <expression>.
If a match occurs, the related <command> is executed. The break statement is treated as in the case label.
regexp(<value>)[:]<command>
The <value> is expected to be a complete standard regular expression that is matched in a case insensitive fashion against <expression>.
If a match occurs, the related <command> is executed. The break statement is treated as in the case label.
default[:]<command>
The default label is executed unconditionally if no previous label terminated execution with the break statement.

Switches

-p | --passthrough
Use c-style 'break' flow control

Syntax Specification

    switch [-p] (<expression>)
    {
        case(<value>)[:]<command>
        [break]
        case(<value>)[:]<command>
        [break]
        ....
        match(<wildcard_expression>)[:]<command>
        [break]
        ....
        regexp(<regular_expression>)[:]<command>
        [break]
        ....
        case(<value>)[:]<command>
        [break]
        ....
        default[:]<command>
        [break]
    }

Examples

# Try to change the 1 below to 2 or 3 to see the results

    %tmp = 1
    switch(%tmp)
    {
        case(1):
            echo \%tmp was 1!
        break;
        case(2)
            echo \%tmp was 2!
        break;
        default:
            echo \%tmp was not 1 nor 2: it was %tmp!
        break;
    }

# A complexier example: change the 1 in 2 or 3

    %tmp = 1
    switch(%tmp)
    {
        case(1):
            echo \%tmp was 1!
        case(2)
            echo \%tmp was 2!
        break;
        default:
            echo \%tmp was either 1 or something different from 2 (%tmp)
        break;
    }

# An example of the -p switch

    %tmp = 1
    switch -p (%tmp)
    {
        case(1):
            echo \%tmp was 1!
        case(2)
            echo \%tmp was 1 or 2!
            break;
        default:
            echo \%tmp was not 1 or 2 (%tmp)
        break;
    }

# An example with strings

    %tmp = "This is a test"
    %tmp2 = "This is not a test"
    switch(%tmp)
    {
        case(%tmp2)
            echo \%tmp == \%tmp2
            break;
        case(%tmp)
        {
            # do not break here
            echo "Yeah.. it's stupid.. \%tmp == \%tmp :D"
        }
        match("*TEST"):
            echo "Matched *TEST"
        regexp("[a-zA-Z ]*test"):
            echo "Matched [a-zA-Z ]*text"
        regexp("[a-zA-Z ]*not[a-zA-Z ]*"):
            echo "Matched [a-zA-Z ]*not[a-zA-Z ]*"
        default:
            echo This is executed anyway (unless some break was called)
        break;
    }

Index, Commands

KVIrc 4.2.0.6190 Documentation
Generated by portbuild at Mon Aug 12 12:09:14 2013