Sujet : Re: misunderstaning of switch command
De : m.n.summerfield (at) *nospam* gmail.com (Mark Summerfield)
Groupes : comp.lang.tclDate : 24. Jun 2025, 09:46:57
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <103dom1$1tams$2@dont-email.me>
References : 1 2
User-Agent : Pan/0.154 (Izium; 517acf4)
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!