1

Normally, switch case in expect looks like this:

switch -- $count \
1 {
set type byte
} 2 {
set type word
} big {
set type array
} default {
puts "$count is not a valid type"
} 

I need to use regex operators such as | or & - how can I do that?

The below example does not work with expect code :

switch -- $variable \
("a"|"b"|"c") {
              do something
}

("a"|"b"|"c") represents a or b or c, but it does not seem to work.

How do I use such statements in switch or may be an and statement?

Adam Adamaszek
  • 3,914
  • 1
  • 19
  • 24
munish
  • 4,505
  • 14
  • 53
  • 83

1 Answers1

4

Use the -regexp option for the command, and brace the expression the Tcl way. Also, you can use braces around all the switches so you don't have to use line continuations.

switch -regexp -- $variable {
    {a|b|c} {
        do something
    }
    {^[def].*(?:g|h|i)} {
        do a different thing
    }
    default {
        do something else
    }
}

http://tcl.tk/man/tcl8.5/TclCmd/switch.htm
http://tcl.tk/man/tcl8.5/TclCmd/re_syntax.htm

glenn jackman
  • 238,783
  • 38
  • 220
  • 352