Re: something like switch in c

From: Darrell Grainger (darrell_at_NOMORESPAMcs.utoronto.ca.com)
Date: 07/08/04


Date: 8 Jul 2004 16:07:44 GMT

On Wed, 7 Jul 2004, lior l wrote:

> Hi,
>
> I wanted to know if there is a way to program ( or there is an
> existing command) that will act like switch but also on strings (char
> *).

You need to specific the programming language. My first assumption is that
you are talking about C or C++. If that is the case then there is no
simple solution for what you want.

The brute force concept would be:

        if(strcmp(...)) {
                :
        } else if(strcmp(...)) {
                :
        } else {
                :
        }

This would have the same layout but not the most efficient way to do
things. The numerous function calls and the cost of each compare could
become quite expensive.

You could create a lookup table but that gets a little more tricky. At
least if you want it to be as readable. Something like:

char lookuptable[][80] = {
        "joe",
        "mike",
        :
};

To make it readable, you need:

enum {JOE, MIKE, ...};

The problem is that if I insert a name in the middle of the lookup table,
I have to insert the name in the enum list as well. Additionally, I still
have to search the list, find the index of the name I'm looking for then
use a switch with the enum values.

The fastest way might be to create a hash table. You pass the string into
a has function and it returns an int. You can then switch on the int. You
will still need the enum (or macros) to make it readable.

The question you have to ask is what is important? Readability,
maintainability, performance, etc.. Find a balance that works for you.

> example:
>
> switch(name)
> {
> case "joe":
> :
> :
> break;
> case "mike":
> :
> :
> break;
> :
> :
> :
>

-- 
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vice.president@whitehouse.gov


Relevant Pages

  • Re: [Switch ()... case]Sting variable
    ... but I would rather use a switch... ... readability to the code; ... Use enum valueOf to convert a String to an enum, ... Roedy Green, Canadian Mind Products ...
    (comp.lang.java.help)
  • Why cant switch be used for objects
    ... The Jit could certainly optimize this case because the addresses of static ... The thing is that is often used my own enum classes because I usually want ... public Color[] GetGreenLikeColors.. ... I see no technical reason why switch shouldn' be allowed for objects. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Reflection and variable selection? Late binding?
    ... If your variables are all integers (or any of the supported base Types for Enums) then use an Enum as in my second example. ... If your variables are other Types such as strings or complex Types then you should use the switch statement. ... when each case is doing something very similar: going from string "x" to ...
    (microsoft.public.dotnet.framework)
  • Re: Long switch statements (and Swing application structural design)
    ... I can't help feeling that I ought to be able to find a way to eliminate the switch statement. ... I don't know off-hand whether you can make an `enum' abstract, ... override it with a concrete method in each constant." ... Any time you find yourself switching or iffing on a type in order to determine which of several versions of the same method you need, you have subverted polymorphism. ...
    (comp.lang.java.programmer)
  • Re: Long switch statements (and Swing application structural design)
    ... case Country: ... case Currency: ... I can't help feeling that I ought to be able to find a way to eliminate the switch statement. ... I don't know off-hand whether you can make an `enum' abstract, ...
    (comp.lang.java.programmer)