Re: switch statement and numeric constants
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.tcl archive

Re: switch statement and numeric constants

From: Cameron Laird <claird@lairds.us>
Date: Fri May 27 2005 - 16:08:02 CEST

In article <1117196919.124006.137200@z14g2000cwz.googlegroups.com>,
Christopher Nelson <cnelson@nycap.rr.com> wrote:
                        .
                        .
                        .
> ...
> binary scan $bpdu "S c c c a*" \
> a(protocol) a(rstpVersion) a(type) a(flags) bpdu
>
> if {$a(protocol) != 0 } {
> error "Invalid protocol"
> }
> switch -- $a(type) {
> 0 {
> set a(type) "CFG"
> }
> 0x80 {
> set a(type) "TCN"
> }
> 2 {
> set a(type) "RST"
> }
> default {
> error "Unknown BPDU type"
> }
> }
> ...
>
>Then the rest of my code and use the fairly natural and expressive
>strings.
>

... and by the time you're writing *this*, it's often natural to
consider the alternative of

    array set my_lookup_table {0 CFG
                               0x80 TCN
                               2 RST}
    set a(type) $my_lookup_table($a(type))

along with a bit of exception-handling.

Also, as Donal notes, bit-pattern-ing can make things messier.
Received on Thu Sep 29 14:19:29 2005