Re: misunderstaning of switch command

Liste des GroupesRevenir à cl tcl 
Sujet : Re: misunderstaning of switch command
De : heller (at) *nospam* deepsoft.com (Robert Heller)
Groupes : comp.lang.tcl
Date : 24. Jun 2025, 13:37:31
Autres entêtes
Organisation : Deepwoods Software
Message-ID : <103e66b$2100k$1@dont-email.me>
References : 1 2 3
User-Agent : TkNews 3.0 (1.2.20)
At Tue, 24 Jun 2025 08:46:57 -0000 (UTC) Mark Summerfield <m.n.summerfield@gmail.com> 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
heller@deepsoft.com       -- Webhosting Services

Date Sujet#  Auteur
24 Jun 25 * misunderstaning of switch command10Mark Summerfield
24 Jun 25 +* Re: misunderstaning of switch command6Harald Oehlmann
24 Jun 25 i`* Re: misunderstaning of switch command5Mark Summerfield
24 Jun 25 i +* Re: misunderstaning of switch command3Harald Oehlmann
24 Jun 25 i i`* Re: misunderstaning of switch command2Harald Oehlmann
24 Jun 25 i i `- Re: misunderstaning of switch command1Alan Grunwald
24 Jun 25 i `- Re: misunderstaning of switch command1Robert Heller
24 Jun 25 +* Re: misunderstaning of switch command2Schelte
24 Jun 25 i`- Re: misunderstaning of switch command1Harald Oehlmann
24 Jun 25 `- Re: misunderstaning of switch command1Robert Heller

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal