I think the usage on vars as const is wrong at first. But are there named constants in tcl??? The second is that I could not avaluate expressions as case value...
The next level of thought is to disconnect from representing things as integers in the first place and instead look at other representations that take advantage of the fact that strings are *the* fundamental datatype of Tcl (by contrast, in C it's really the machine-word). That's quite a change though, and probably requires a lot of reworking of the rest of your program too.
> The next level of thought is to disconnect from representing things as > integers in the first place and instead look at other representations > that take advantage of the fact that strings are *the* fundamental > datatype of Tcl (by contrast, in C it's really the machine-word).
Oh, I don't know. I think:
switch -- $something { FOO { } BAR { } }
is fairly close to the C:
#define FOO 1 #define BAR 2
switch (something) { case FOO: break; case BAR: break; }
I re-remembered this recently writing some Tcl to parse 802.1D BPDUs:
... binary scan $bpdu "S c c c a*" \ a(protocol) a(rstpVersion) a(type) a(flags) bpdu
* "Klaus" <lts-rudo...@gmx.de> | the tcl code i tried was: | set VAL_A 1 | set VAL_B 2 | set VAL_C 4 | | switch -regexp expr( x & VAL_A|VAL_C) \ | $VAL_A { } \ | $VAL_C|$VAL_C {} \ | | but that did not work.
What should that "$VAL_C|$VAL_C" mean?
Even the original case VAL_A: ... break; case VAL_A|VAL_C: break; does not make much sense in that order (VAL_A will always trigger in the first clause, the second will never run).
The "|" in C-case is best thought of as the foo - bar { } in TCL-switch, so try switch -- [expr {$x & ($VAL_A|$VAL_C)}] \ $VAL_A { puts VAL-A } \ SVAL_B - \ $VAL_C { puts "VAL-B (will never trigger) | VAL-C" }
Note: the precedence of & and | in TCl-expr are the same as in C, so you need the same grouping in TCL-expr as in C-switch.
Ralf Fassel wrote: > Even the original > case VAL_A: ... break; > case VAL_A|VAL_C: break; > does not make much sense in that order (VAL_A will always trigger in > the first clause, the second will never run).
Wrong. VAL_A|VAL_C is an expression with constant value 5, whereas VAL_A on its own is an expression with constant value 1.
You are aware that the switch command does string matching? This means that the 0x80 pattern will never match the value of a variable that has been set by binary scan c. Your pattern should probably be "-128".
Schelte. -- set Reply-To [string map {nospam schelte} $header(From)]