Home > Archive > Tcl > January 2006 > Problem with a Switch test
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Problem with a Switch test
|
|
|
| Hello,
I want to use switch, but it doesn't work, my problem is like this :
set $Var \x30\x18
proc NameProc { Test } {
global Var
upvar $Test LocalTest
switch $LocalTest {
$Var {
#Do something - It doesn't pass here
}
\x30\x18 {
#Do something else - It works if $LocalTest == \x30\x18
}
default {
#Do something else - here it works if $LocalTest != \x30\x18
}
}
}
I don't understand why it doesn't work when it tests with $Var
| |
| Gerald W. Lester 2006-01-31, 7:59 am |
| RV wrote:
> Hello,
>
> I want to use switch, but it doesn't work, my problem is like this :
>
> set $Var \x30\x18
>
> proc NameProc { Test } {
> global Var
> upvar $Test LocalTest
>
> switch $LocalTest {
>
> $Var {
> #Do something - It doesn't pass here
> }
>
> \x30\x18 {
> #Do something else - It works if $LocalTest == \x30\x18
> }
>
> default {
> #Do something else - here it works if $LocalTest != \x30\x18
> }
>
> }
> }
>
> I don't understand why it doesn't work when it tests with $Var
>
Please read http://www.tcl.tk/man/tcl8.4/TclCmd/Tcl.htm; pay attention to
what braces are and what can and can not happen inside of them. Do not
assume that **anything** other than what that page states is going on inside
of Tcl.
Hint:: Tcl is not C/C++ or Java -- the syntax rules for those languages do
not apply.
| |
| Bryan Oakley 2006-01-31, 7:03 pm |
| RV wrote:
> Hello,
>
> I want to use switch, but it doesn't work, my problem is like this :
>
> set $Var \x30\x18
>
> proc NameProc { Test } {
> global Var
> upvar $Test LocalTest
>
> switch $LocalTest {
>
> $Var {
> #Do something - It doesn't pass here
> }
> ...
>
> I don't understand why it doesn't work when it tests with $Var
>
From the switch man page:
"Since the pattern arguments are in braces in the second form, no
command or variable substitutions are performed on them"
Switch has two forms, though, so maybe you want to switch to the other form:
switch $LocalTest \
$var {doSomething} \
\x30\x18 {doSomethingElse} \
...
There are other ways to solve the problem, but simply changing the form
of switch you are using is the most straight-forward.
--
Bryan Oakley
http://www.tclscripting.com
| |
|
| Yes, it works effectively with the simple form
thanks
but, can I use the default option ? It answers me a "unknown" error
with it
| |
| Glenn Jackman 2006-01-31, 7:03 pm |
| At 2006-01-31 08:24AM, RV <rvlebars@hotmail.com> wrote:
> Hello,
>
> I want to use switch, but it doesn't work, my problem is like this :
>
> set $Var \x30\x18
-------^
Tcl is not Perl:
set Var \x30\x18
--
Glenn Jackman
Ulterior Designer
| |
|
| Yes, sorry, a mistake
and to talk about this, I have been coding with TCL and Perl for few
mounth, and now I have the dream that someone create the most powerful
coding langage wich will be the association of both. It will be so
great.
For moment, I like Hash Table in Perl to make easily your own Database,
and String functions, the simplest way to include our own packaging...
for TCL, I like Variable trace, the "after" function, and the
simplicity of using events with widget linked variables
| |
| Gerry Snyder 2006-01-31, 7:03 pm |
| RV wrote:
> Yes, it works effectively with the simple form
>
> thanks
>
> but, can I use the default option ? It answers me a "unknown" error
> with it
>
Both forms are fine. Neither is the default. The one you tried is,
perhaps, used more often than the other because we tend to forget to add
the backslash at the end of a line, leading to a bug which can be very
hard to see.
But the answer is that you cannot do what you want. Substitution does
not happen inside curly braces.
Gerry
| |
| Michael Schlenker 2006-01-31, 7:03 pm |
| RV schrieb:
> Yes, sorry, a mistake
>
> and to talk about this, I have been coding with TCL and Perl for few
> mounth, and now I have the dream that someone create the most powerful
> coding langage wich will be the association of both. It will be so
> great.
> For moment, I like Hash Table in Perl to make easily your own Database,
> and String functions, the simplest way to include our own packaging...
Why bother with implementing your own database with hashes when you can
have the excellent sqlite in under 200kB?
What do you mean with 'simplest way to include our own packaging'?
Michael
| |
| Bryan Oakley 2006-01-31, 7:03 pm |
| RV wrote:
> Yes, it works effectively with the simple form
>
> thanks
>
> but, can I use the default option ? It answers me a "unknown" error
> with it
>
Show the code and the error. Yes, you can use "default" as one of the
patterns.
--
Bryan Oakley
http://www.tclscripting.com
| |
| Bruce Hartweg 2006-01-31, 7:03 pm |
|
RV wrote:
> Yes, it works effectively with the simple form
>
> thanks
>
> but, can I use the default option ? It answers me a "unknown" error
> with it
>
Yes you can.
Are you sure the default option is part of the same command ?
Note that all of the line in this form need continuation chars
at the end so that the switch command is one long logical line.
If you forgot the \ at the end of the previous line, then
the interp will have already completed the switch command and
is now trying to run a new command called "default"
Bruce
| |
| Bruce Hartweg 2006-01-31, 7:03 pm |
|
RV wrote:
> Yes, sorry, a mistake
>
> and to talk about this, I have been coding with TCL and Perl for few
> mounth, and now I have the dream that someone create the most powerful
> coding langage wich will be the association of both. It will be so
> great.
> For moment, I like Hash Table in Perl to make easily your own Database,
> and String functions, the simplest way to include our own packaging...
> for TCL, I like Variable trace, the "after" function, and the
> simplicity of using events with widget linked variables
>
Arrays in Tcl are hash tables (also dict are hash tabels that
are also valus that can be passed around)
Bruce
| |
|
| As I have already said,
I don't have been coding in TCL for very long time
But "sqlite" is surely something that will interest me, I find that
Perl hashes have an easy form and are easy to use to make little and
simple databases, but SQlite may be something that could like to me.
If you have more informations on this ...
| |
|
|
| Donal K. Fellows 2006-01-31, 7:03 pm |
| Bryan Oakley wrote:
> Switch has two forms, though, so maybe you want to switch to the other
> form:
>
> switch $LocalTest \
> $var {doSomething} \
> \x30\x18 {doSomethingElse} \
> ...
>
> There are other ways to solve the problem, but simply changing the form
> of switch you are using is the most straight-forward.
The easiest way of writing that is:
switch -- $LocalTest $var {
doSomething
} \x30\x18 {
doSomethingElse ...
}
By putting the braces in the right spots, you can get rid of the
backslashes and almost forget that you're not using the common form of
[switch] command.
Donal.
|
|
|
|
|