Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
switch statement and numeric constants
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  9 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Klaus  
View profile  
 More options May 27 2005, 5:11 am
Newsgroups: comp.lang.tcl
From: "Klaus" <lts-rudo...@gmx.de>
Date: 27 May 2005 02:11:37 -0700
Local: Fri, May 27 2005 5:11 am
Subject: switch statement and numeric constants
Hi all,

I want to the same thing in tcl as the following C code:

#define VAL_A 1
#define VAL_B 2
#define VAL_C 4

int x = VAL_A|VAL_C;

switch (x & (VAL_A|VAL_C)) {
    case VAL_A: ... break;
    case VAL_A|VAL_C: break;

}

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.

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...

Any hint?

Bye
   Klaus


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Donal K. Fellows  
View profile  
 More options May 27 2005, 6:20 am
Newsgroups: comp.lang.tcl
From: "Donal K. Fellows" <donal.k.fell...@manchester.ac.uk>
Date: Fri, 27 May 2005 11:20:55 +0100
Local: Fri, May 27 2005 6:20 am
Subject: Re: switch statement and numeric constants

Klaus wrote:
> switch -regexp expr( x & VAL_A|VAL_C) \
>      $VAL_A { } \
>      $VAL_C|$VAL_C {} \

You probably want something like this:

   switch -exact -- [expr {$x & ($VAL_A|$VAL_C)}] \
         $VAL_A                 { ... } \
         [expr {$VAL_A|$VAL_C}] { ... }

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.

Donal.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Christopher Nelson  
View profile  
 More options May 27 2005, 8:28 am
Newsgroups: comp.lang.tcl
From: "Christopher Nelson" <cnel...@nycap.rr.com>
Date: 27 May 2005 05:28:39 -0700
Local: Fri, May 27 2005 8:28 am
Subject: Re: switch statement and numeric constants

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

    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.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Donal K. Fellows  
View profile  
 More options May 27 2005, 8:48 am
Newsgroups: comp.lang.tcl
From: "Donal K. Fellows" <donal.k.fell...@manchester.ac.uk>
Date: Fri, 27 May 2005 13:48:55 +0100
Local: Fri, May 27 2005 8:48 am
Subject: Re: switch statement and numeric constants

Christopher Nelson wrote:
> Oh, I don't know.  I think:
>     switch -- $something {
>        FOO {
>        }
>        BAR {
>        }
>     }
> is fairly close to the C:

That'd be fine (even highly recommended), except he's switching on bit
patterns and not simple symbolic constants. That makes things much messier.

Donal.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ralf Fassel  
View profile  
 More options May 27 2005, 9:21 am
Newsgroups: comp.lang.tcl
From: Ralf Fassel <ralf...@gmx.de>
Date: Fri, 27 May 2005 15:21:55 +0200
Local: Fri, May 27 2005 9:21 am
Subject: Re: switch statement and numeric constants
* "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.

HTH
R'


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Donal K. Fellows  
View profile  
 More options May 27 2005, 9:30 am
Newsgroups: comp.lang.tcl
From: "Donal K. Fellows" <donal.k.fell...@manchester.ac.uk>
Date: Fri, 27 May 2005 14:30:41 +0100
Local: Fri, May 27 2005 9:30 am
Subject: Re: switch statement and numeric constants

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.

Donal.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Schelte Bron  
View profile  
 More options May 27 2005, 9:58 am
Newsgroups: comp.lang.tcl
From: Schelte Bron <nos...@wanadoo.nl>
Date: Fri, 27 May 2005 15:58:40 +0200
Local: Fri, May 27 2005 9:58 am
Subject: Re: switch statement and numeric constants
On 05/27/05 14:28, Christopher Nelson wrote:

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)]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Cameron Laird  
View profile  
 More options May 27 2005, 10:08 am
Newsgroups: comp.lang.tcl
From: cla...@lairds.us (Cameron Laird)
Date: Fri, 27 May 2005 14:08:02 GMT
Local: Fri, May 27 2005 10:08 am
Subject: Re: switch statement and numeric constants
In article <1117196919.124006.137...@z14g2000cwz.googlegroups.com>,
Christopher Nelson <cnel...@nycap.rr.com> wrote:

                        .
                        .
                        .

... 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.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Klaus Rudolph  
View profile  
 More options May 27 2005, 11:51 am
Newsgroups: comp.lang.tcl
From: Klaus Rudolph <lts-rudo...@gmx.de>
Date: Fri, 27 May 2005 17:51:34 +0200
Local: Fri, May 27 2005 11:51 am
Subject: Re: switch statement and numeric constants
Donal K. Fellows schrieb:

> Klaus wrote:

>> switch -regexp expr( x & VAL_A|VAL_C) \
>>      $VAL_A { } \
>>      $VAL_C|$VAL_C {} \

> You probably want something like this:

>   switch -exact -- [expr {$x & ($VAL_A|$VAL_C)}] \
>         $VAL_A                 { ... } \
>         [expr {$VAL_A|$VAL_C}] { ... }

That is what I want!
Thanks a lot!

        Klaus


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google