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?
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
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!
The "if" command takes his first argument and passes it to "expr".
Then, eval will do the variable expansion.
Then, expr will do the variable expansion.
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.
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?
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.
I have a switch command which is doing something I don't expect but Iswitch does NOT evaluate the case labels. That is, the case labels are *literally*:
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?
On Tue, 24 Jun 2025 10:37:08 +0200, Harald Oehlmann wrote: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.
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!
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 1,064 |
Nodes: | 10 (0 / 10) |
Uptime: | 163:56:39 |
Calls: | 13,691 |
Calls today: | 1 |
Files: | 186,936 |
D/L today: |
9,208 files (2,741M bytes) |
Messages: | 2,411,516 |