switch using -matchvar

Whatswitch using -matchvar
Descriptionwhat is the -matchvar for
Wherehttps://www.tcl-lang.org/man/tcl/TclCmd/switch.htm
Requires
tagsswitch matchvar
Updated21.10.2011

I was looking for a solution for a problem like this:

set value_01 {abc}
set value_02 {bcd}
       
foreach value {abc bcd cde cdde def deeef any} {
    $value_01 {...}
    $value_02 {...}
    default   {...}
}

there is an example in the manual. I hoped this can help me, but it does not work.

https://www.tcl-lang.org/man/tcl/TclCmd/switch.htm

set foo "abc"

switch -regexp -matchvar foo -- $bar {
    a(b*)c {
        puts "Found [string length [lindex $foo 1]] 'b's"
    }
    d(e*)f(g*)h {
        puts "Found [string length [lindex $foo 1]] 'e's and\
                [string length [lindex $foo 2]] 'g's"
    }
}

this example raise an error:

can't read "bar": no such variable

so whats on?

I did some trials: First on the switch: "-matchvar" to correct above example

I had to change the use of the variables $bar and $foo:

  • $bar is not set
  • $foo is the matching varible inside the switch control
set foo "abc"
catch {unset bar}

switch -regexp -matchvar bar -- $foo {
   a(b*)c {
      puts "Found [string length [lindex $bar 1]] 'b's"
   }
   d(e*)f(g*)h {
      puts "Found [string length [lindex $bar 1]] 'e's and\
         [string length [lindex $bar 2]] 'g's"
   }
}

this should bring the following result:

Found 1 'b's

and the solution for my problem looks like this:

set value_01 {abc}
set value_02 {bcd}

foreach value {abc bcd cde cdde def deeef any} {
    switch -regexp -matchvar foo -- $value \
        $value_01 {
                puts "\n    ... \$value_01: $value_01"
                puts   "    ...... \$value:   $value"
                puts   "    ...... \$foo:     $foo"
        } \
        $value_02 {
                puts "\n    ... \$value_02: $value_02"
                puts "      ...... \$value:   $value"
                puts "      ...... \$foo:     $foo"
        } \
        c(d*)e {
                puts "\n    ... c(d*)e"
                puts "      ...... \$value: $value"
                puts "      ...... \$foo:   $foo"
        } \
        d(e*)f {
                puts "\n    ... d(e*)f"
                puts "      ...... \$value: $value"
                puts "      ...... \$foo:   $foo"
        } \
        default {
                puts "\n    ... default"
                puts "      ...... \$value: $value"
                puts "      ...... \$foo:   $foo"
            }         
}

this should bring the following result:

... $value_01: abc
...... $value:   abc
...... $foo:     abc

... $value_02: bcd
  ...... $value:   bcd
  ...... $foo:     bcd

... c(d*)e
  ...... $value: cde
  ...... $foo:   cde d

... c(d*)e
  ...... $value: cdde
  ...... $foo:   cdde dd

... d(e*)f
  ...... $value: def
  ...... $foo:   def e

... d(e*)f
  ...... $value: deeef
  ...... $foo:   deeef eee

... default
  ...... $value: any
  ...... $foo:

I hope this shows the aim of the "-matchvar" switch