• Re: misunderstaning of switch command

    From Harald Oehlmann@21:1/5 to All on Tue Jun 24 10:37:08 2025
    Am 24.06.2025 um 10:23 schrieb Mark Summerfield:
    I have a switch command which is doing something I don't expect but I
    don't understand what I've done wrong. In this example the default is
    always executed but I expect the case before that to be executed.

    const UNCOMPRESSED U
    const ZLIB_COMPRESSED Z
    const SAME_AS_PREV =
    set filename somefile.txt
    set action "added"
    set kind Z
    switch $kind {
    $::SAME_AS_PREV { puts "unchanged \"$filename\"" }
    $::UNCOMPRESSED { puts "$action \"$filename\"" }
    $::ZLIB_COMPRESSED { puts "$action \"$filename\" (zlib
    compressed)" }
    default { puts "!!!!!!!! UNEXPECTED !!!!!!!!" }
    }

    What am I doing wrong?

    Nothing wrong. Tcl is just different, sorry for that.

    The "{" always avoids expansion of variables and commands. If you want
    to use variables in the switch, you have to avoid the "{".

    switch -exact -- $kind [list\
    $::SAME_AS_PREV { puts "unchanged \"$filename\"" }\
    $::UNCOMPRESSED { puts "$action \"$filename\"" }
    $::ZLIB_COMPRESSED { puts "$action \"$filename\" (zlib
    compressed)" }\
    default { puts "!!!!!!!! UNEXPECTED !!!!!!!!" }\
    ]

    Nevertheless, this is no fun on the quoting level (backslashes at the
    end etc). In addition, you have to take care, when the variable
    expansion happens. This might be tricky.

    I personally would write it like that:

    switch -exact -- $kind {
    = { # SAME_AS_PREV
    puts "unchanged \"$filename\""
    }
    U { # UNCOMPRESSED
    puts "$action \"$filename\""
    }
    Z { # ZLIB_COMPRESSED
    puts "$action \"$filename\" (zlib compressed)"
    }
    default { puts "!!!!!!!! UNEXPECTED !!!!!!!!" }
    }

    Harald

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Harald Oehlmann@21:1/5 to All on Tue Jun 24 10:58:17 2025
    Am 24.06.2025 um 10:46 schrieb Mark Summerfield:
    On Tue, 24 Jun 2025 10:37:08 +0200, Harald Oehlmann wrote:

    Am 24.06.2025 um 10:23 schrieb Mark Summerfield:
    I have a switch command which is doing something I don't expect but I
    don't understand what I've done wrong. In this example the default is
    always executed but I expect the case before that to be executed.

    const UNCOMPRESSED U const ZLIB_COMPRESSED Z const SAME_AS_PREV =
    set filename somefile.txt set action "added"
    set kind Z switch $kind {
    $::SAME_AS_PREV { puts "unchanged \"$filename\"" }
    $::UNCOMPRESSED { puts "$action \"$filename\"" }
    $::ZLIB_COMPRESSED { puts "$action \"$filename\" (zlib
    compressed)" }
    default { puts "!!!!!!!! UNEXPECTED !!!!!!!!" }
    }

    What am I doing wrong?

    Nothing wrong. Tcl is just different, sorry for that.

    The "{" always avoids expansion of variables and commands. If you want
    to use variables in the switch, you have to avoid the "{".

    switch -exact -- $kind [list\
    $::SAME_AS_PREV { puts "unchanged \"$filename\"" }\
    $::UNCOMPRESSED { puts "$action \"$filename\"" }
    $::ZLIB_COMPRESSED { puts "$action \"$filename\" (zlib
    compressed)" }\
    default { puts "!!!!!!!! UNEXPECTED !!!!!!!!" }\
    ]

    Nevertheless, this is no fun on the quoting level (backslashes at the
    end etc). In addition, you have to take care, when the variable
    expansion happens. This might be tricky.

    I personally would write it like that:

    switch -exact -- $kind {
    = { # SAME_AS_PREV
    puts "unchanged \"$filename\""
    }
    U { # UNCOMPRESSED
    puts "$action \"$filename\""
    }
    Z { # ZLIB_COMPRESSED
    puts "$action \"$filename\" (zlib compressed)"
    }
    default { puts "!!!!!!!! UNEXPECTED !!!!!!!!" }
    }

    Harald

    Thanks that works great.

    Bit of a disinsentive to use consts though!

    Great, that it works for you.

    If I want to compare something with multiple variables, I would use an
    if chain:

    if {$kind eq $::SAME_AS_PREV} {
    puts "unchanged \"$filename\""
    } else if {$kind eq $::UNCOMPRESSED} {
    puts "$action \"$filename\""
    } else if {$kind eq $::ZLIB_COMPRESSED} {
    puts "$action \"$filename\" (zlib >> compressed)"
    } else {
    puts "!!!!!!!! UNEXPECTED !!!!!!!!"
    }

    The "if" command takes his first argument and passes it to "expr".
    Then, eval will do the variable expansion.
    This does not happen with "switch".

    I never used const. But its use may help for clarity.

    Harald

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Harald Oehlmann@21:1/5 to All on Tue Jun 24 11:08:24 2025
    Sorry, typo in my last post:

    The "if" command takes his first argument and passes it to "expr".
    Then, eval will do the variable expansion.

    Correct:
    Then, expr will do the variable expansion.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Schelte@21:1/5 to Mark Summerfield on Tue Jun 24 12:53:57 2025
    On 24/06/2025 10:23, Mark Summerfield wrote:
    I have a switch command which is doing something I don't expect but I
    don't understand what I've done wrong. In this example the default is
    always executed but I expect the case before that to be executed.

    const UNCOMPRESSED U
    const ZLIB_COMPRESSED Z
    const SAME_AS_PREV =
    set filename somefile.txt
    set action "added"
    set kind Z
    switch $kind {
    $::SAME_AS_PREV { puts "unchanged \"$filename\"" }
    $::UNCOMPRESSED { puts "$action \"$filename\"" }
    $::ZLIB_COMPRESSED { puts "$action \"$filename\" (zlib
    compressed)" }
    default { puts "!!!!!!!! UNEXPECTED !!!!!!!!" }
    }

    What am I doing wrong?

    Switch allows for two different ways to specify the pattern/body pairs:
    As as a single argument or as individual arguments. If you want the
    patterns to be in variables (or consts) you may prefer the latter
    method. This can be written as:

    switch $kind \
    $::SAME_AS_PREV {
    puts "unchanged \"$filename\""
    } $::UNCOMPRESSED {
    puts "$action \"$filename\""
    } $::ZLIB_COMPRESSED {
    puts "$action \"$filename\" (zlib compressed)"
    } default {
    puts "!!!!!!!! UNEXPECTED !!!!!!!!"
    }


    Schelte.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Harald Oehlmann@21:1/5 to All on Tue Jun 24 14:06:14 2025
    Am 24.06.2025 um 12:53 schrieb Schelte:
    On 24/06/2025 10:23, Mark Summerfield wrote:
    I have a switch command which is doing something I don't expect but I
    don't understand what I've done wrong. In this example the default is
    always executed but I expect the case before that to be executed.

         const UNCOMPRESSED U
         const ZLIB_COMPRESSED Z
         const SAME_AS_PREV =
         set filename somefile.txt
         set action "added"
         set kind Z
         switch $kind {
             $::SAME_AS_PREV { puts "unchanged \"$filename\"" }
             $::UNCOMPRESSED { puts "$action \"$filename\"" }
             $::ZLIB_COMPRESSED { puts "$action \"$filename\" (zlib
    compressed)" }
             default { puts "!!!!!!!! UNEXPECTED !!!!!!!!" }
         }

    What am I doing wrong?

    Switch allows for two different ways to specify the pattern/body pairs:
    As as a single argument or as individual arguments. If you want the
    patterns to be in variables (or consts) you may prefer the latter
    method. This can be written as:

    switch $kind \
      $::SAME_AS_PREV {
        puts "unchanged \"$filename\""
    } $::UNCOMPRESSED {
        puts "$action \"$filename\""
    } $::ZLIB_COMPRESSED {
        puts "$action \"$filename\" (zlib compressed)"
    } default {
        puts "!!!!!!!! UNEXPECTED !!!!!!!!"
    }


    Schelte.


    RIGHT YOU ARE !

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Robert Heller@21:1/5 to Mark Summerfield on Tue Jun 24 12:37:31 2025
    At Tue, 24 Jun 2025 08:46:57 -0000 (UTC) Mark Summerfield <[email protected]> wrote:


    On Tue, 24 Jun 2025 10:37:08 +0200, Harald Oehlmann wrote:

    Am 24.06.2025 um 10:23 schrieb Mark Summerfield:
    I have a switch command which is doing something I don't expect but I
    don't understand what I've done wrong. In this example the default is
    always executed but I expect the case before that to be executed.

    const UNCOMPRESSED U const ZLIB_COMPRESSED Z const SAME_AS_PREV > >> set filename somefile.txt set action "added"
    set kind Z switch $kind {
    $::SAME_AS_PREV { puts "unchanged \"$filename\"" }
    $::UNCOMPRESSED { puts "$action \"$filename\"" }
    $::ZLIB_COMPRESSED { puts "$action \"$filename\" (zlib
    compressed)" }
    default { puts "!!!!!!!! UNEXPECTED !!!!!!!!" }
    }

    What am I doing wrong?

    Nothing wrong. Tcl is just different, sorry for that.

    The "{" always avoids expansion of variables and commands. If you want
    to use variables in the switch, you have to avoid the "{".

    switch -exact -- $kind [list\
    $::SAME_AS_PREV { puts "unchanged \"$filename\"" }\
    $::UNCOMPRESSED { puts "$action \"$filename\"" }
    $::ZLIB_COMPRESSED { puts "$action \"$filename\" (zlib compressed)" }\
    default { puts "!!!!!!!! UNEXPECTED !!!!!!!!" }\
    ]

    Nevertheless, this is no fun on the quoting level (backslashes at the
    end etc). In addition, you have to take care, when the variable
    expansion happens. This might be tricky.

    I personally would write it like that:

    switch -exact -- $kind {
    = { # SAME_AS_PREV
    puts "unchanged \"$filename\""
    }
    U { # UNCOMPRESSED
    puts "$action \"$filename\""
    }
    Z { # ZLIB_COMPRESSED
    puts "$action \"$filename\" (zlib compressed)"
    }
    default { puts "!!!!!!!! UNEXPECTED !!!!!!!!" }
    }

    Harald

    Thanks that works great.

    Bit of a disinsentive to use consts though!

    Yes. You really can't use them the way you do in C/C++. Tcl does not really have "const". It also really does not have "statements". Tcl is like LISP in many ways.




    --
    Robert Heller -- Cell: 413-658-7953 GV: 978-633-5364
    Deepwoods Software -- Custom Software Services
    http://www.deepsoft.com/ -- Linux Administration Services
    [email protected] -- Webhosting Services

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)