Switch doesn't variable substitution
Closed / Invalid1537233fffffffffffffffffffffffffffffffffCreated 20 years agoUpdated 20 years ago
Descriptionplain2006-08-09 08:57:41
Hi again guys,
I've seen a feature request that can be related
(1106138) so probably it's a known "feature" of the
switch command.
Anyway I find it quite strange.
Here is the script that shows the actual problem:
# Create some variables and assign numbers to them
set one 1
set two 2
set three 3
set value 2
switch -- $value {
$one {puts 1}
$two {puts 2}
$three {puts 3}
default {puts "$value is unknown"}
}
% 2 is unknown
switch -- $value {
1 {puts 1}
2 {puts 2}
3 {puts 3}
default {puts "$value is unknown"}
}
% 2
As you can see looks like is not doing the variable
substitution. Googling a bit I found this solution:
switch -- $value [list\
$one {puts 1}\
$two {puts 2}\
$three {puts 3}\
default {puts "$value is unknown"}\
]
% 2
Which works, but is quite dirty.
Should be considered a bug?
dkfplain2006-08-09 17:04:23
Logged In: YES
user_id=79902
It's not a bug. It's just Tcl's standard quoting rules, and
everything *has* to be this way for reasons that are rather
deeply embedded in the language's basic philosophy.
But for this case, the other form of input to the [switch]
command is probably useful. You are aware that what you
wrote could also be (wholly equivalently) written like this:
switch -- $value \
$one {puts 1} \
$two {puts 2} \
$three {puts 3} \
default {puts "$value is unknown"}
Hope this helps.
ctasadaplain2006-08-09 17:18:29
Logged In: YES user_id=340696 Ok, I though you would say it ;) Anyway would be nice to have this kind of feature is Tcl ;) Thanks a lot.
Log in (anonymous is fine) to add a comment.