Re: Why don't variables work in switch patterns?



Ralf Fassel wrote:
* Bruce <news@xxxxxxxxxxx>
| to parser - that outer word is quoted with {} so the whole thing is
| passed as is to the switch statement - which doesn't do any extra
| substitutions on the pieces.

The confusion might come from the fact that you _can_ use variables
inside the actions, but not in the patterns, if the {} form is used.

Eg.
switch -- $value {
foo {
# can use variables here, but not in the pattern line directly above
puts "value is $value"
}
}

But that is clearly stated in the man pages. In this case the interpreter
does NOT substitute in either the pattern or bodies, they are passed literally
to the switch command which does t he following:

The switch command matches its string argument against each of the pattern arguments in order. As soon as it finds a pattern that matches string it evaluates the following body argument by passing it recursively to the Tcl interpreter and returns the result of that evaluation.

(quoted from http://www.tcl.tk/man/tcl8.4/TclCmd/switch.htm)

so the is no magic goping on - initially NO substitution occurs, then is a pattern
is matched the body associated is passed again to the interpreter - this is when
the variables are/commands are substituted.

Bruce
.



Relevant Pages

  • Re: http site text-only read possible?
    ... >> If you switch off image display then the image files aren't fetched ... The -O switch sends the downloaded file to the file "foo.txt" ... The pattern in foo.awk matches all lines from a line containing the ... Finally a Fortran program reads and does something with the data ...
    (comp.lang.fortran)
  • Re: Comparing memory with a constant
    ... switch (array) { ... case bitpattern1: ... ... matched pattern #0 ... ... for computing a number to switch on, ...
    (comp.lang.c)
  • Re: Advice on landing better
    ... and did two passes in the pattern. ... It is as if the hand just reaches out on its own and hits the switch. ... All you probably really need is practice, perhaps with some supervision so that your instructor can tell you exactly what you need to improve and so you don't practice bad habits. ... World Famous Flight Instructor ...
    (rec.aviation.student)
  • Re: Your juggling weaknesses.
    ... Mark Nicoll wrote: ... barrier that causes me to just switch off around 250 catches in any ... pattern with any number of balls. ... I start a stopwatch, then count five ...
    (rec.juggling)
  • Re: Why dont variables work in switch patterns?
    ... | The switch command matches its string argument against each of the ... | pattern arguments in order. ... command or variable substitutions are performed on them; ...
    (comp.lang.tcl)