switch on strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Keith Thompson

    #16
    Re: switch on strings

    Old Wolf <oldwolf@inspir e.net.nzwrites:
    On Sep 27, 12:08 am, CBFalconer <cbfalco...@yah oo.comwrote:
    >Well done. Now maybe the lesson will stick. Add to it the need
    >for propper snipping, especially of sigs.
    >>
    >--
    > Chuck F (cbfalconer at maineline dot net)
    > Available for consulting/temporary embedded and systems.
    > <http://cbfalconer.home .att.net>
    >>
    >--
    >Posted via a free Usenet account fromhttp://www.teranews.co m
    >
    Physician, heal thyself..
    Chuck doesn't have a problem with snipping of sigs, i.e., with
    deleting signatures from quoted material. If he criticized others for
    having signatures that are too long, or that have embedded "-- "
    lines, your argument would be stronger.

    --
    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

    Comment

    • iesvs@free.fr

      #17
      Re: switch on strings

      Maybe we should all club together and give Chuck an annual $29.95
      standard Teranews account for Christmas... I'll pledge the first $1 :)
      You pay to use the news ? Really ?

      Comment

      • Default User

        #18
        Re: switch on strings

        iesvs@free.fr wrote:
        Maybe we should all club together and give Chuck an annual $29.95
        standard Teranews account for Christmas... I'll pledge the first $1
        :)
        >
        You pay to use the news ? Really ?
        Yes. Google Groups is one of the worst ways to access newsgroups. It's
        really, really, bad.

        A real news service and dedicated newsreader are far superior.




        Brian

        Comment

        • Mark McIntyre

          #19
          Re: switch on strings

          On Sat, 29 Sep 2007 11:19:53 +0530, in comp.lang.c , santosh
          <santosh.k83@gm ail.comwrote:
          >Err no. There are free news servers too. And not just aioe.org. He could use
          >one of those.
          TAANSTAFL.

          I have a "free" news server as part of my ISP deal. Its about as much
          use as a chocolate teapot.

          But hey, picking on Chuck because of something beyond his reasonable
          control is way more fun than answering actual questions about C. so
          why not continue?
          --
          Mark McIntyre

          "Debugging is twice as hard as writing the code in the first place.
          Therefore, if you write the code as cleverly as possible, you are,
          by definition, not smart enough to debug it."
          --Brian Kernighan

          Comment

          • santosh

            #20
            Re: switch on strings

            Mark McIntyre wrote:
            On Sat, 29 Sep 2007 11:19:53 +0530, in comp.lang.c , santosh
            <santosh.k83@gm ail.comwrote:
            >
            >>Err no. There are free news servers too. And not just aioe.org. He could
            >>use one of those.
            >
            TAANSTAFL.
            >
            I have a "free" news server as part of my ISP deal. Its about as much
            use as a chocolate teapot.
            In my experience, I've found motrazella.org to be quite responsive. It needs
            a registration, but is otherwise free. But, as another poster to this
            thread noted, Chuck apparently posts hundreds of messages a day, so it may
            be that free offerings are ill-suited for him.
            But hey, picking on Chuck because of something beyond his reasonable
            control is way more fun than answering actual questions about C. so
            why not continue?
            Well, for my part, I've not raised any issue WRT Chuck's sig[s]. The
            previous post was a response to your statement up-thread.

            Comment

            • Spiros Bousbouras

              #21
              Re: switch on strings

              On 27 Sep, 13:11, CBFalconer <cbfalco...@yah oo.comwrote:
              There is no prohibition against a sig
              marker appearing within a sig.
              So how is a newsreader supposed to know how much to snip then ?

              Comment

              • santosh

                #22
                Re: switch on strings

                Spiros Bousbouras wrote:
                On 27 Sep, 13:11, CBFalconer <cbfalco...@yah oo.comwrote:
                >There is no prohibition against a sig
                >marker appearing within a sig.
                >
                So how is a newsreader supposed to know how much to snip then ?
                Presumably snip all text after the first instance of sig delimiter?

                Comment

                • Mark McIntyre

                  #23
                  Re: switch on strings

                  On Sat, 29 Sep 2007 07:00:38 -0700, in comp.lang.c , Spiros Bousbouras
                  <spibou@gmail.c omwrote:
                  >On 27 Sep, 13:11, CBFalconer <cbfalco...@yah oo.comwrote:
                  >There is no prohibition against a sig
                  >marker appearing within a sig.
                  >
                  >So how is a newsreader supposed to know how much to snip then ?
                  line = readline()
                  while (line != NULL)
                  if (left(line, 3) == "-- ") then goto nomorepost
                  putline(">" & line)
                  line = readline()
                  wend
                  nomorepost:

                  --
                  Mark McIntyre

                  "Debugging is twice as hard as writing the code in the first place.
                  Therefore, if you write the code as cleverly as possible, you are,
                  by definition, not smart enough to debug it."
                  --Brian Kernighan

                  Comment

                  • Lew Pitcher

                    #24
                    Re: switch on strings

                    On Sep 25, 2:20 am, user923005 <dcor...@connx. comwrote:
                    On Sep 24, 11:19 pm, sinbad <sinbad.sin...@ gmail.comwrote:
                    >
                    how to implement a switch case for stringse...swit ch should happen
                    depending on the strings...
                    >
                    Suggestion: do a web search for "perfect hash".
                    >
                    From the C FAQ:
                    >
                    20.17: Is there a way to switch on strings?
                    >
                    A: Not directly. Sometimes, it's appropriate to use a separate
                    function to map strings to integer codes, and then switch on
                    those. Otherwise, of course, you can fall back on strcmp()
                    [snip]

                    For example....

                    #include <stdarg.h>
                    #include <stdlib.h>

                    /*
                    ** string compare string Match to list of strings starting with Model
                    ** return ordinal of successful match (>=0) or -1 on no match
                    */

                    int WhichString(cha r *Match, ...)
                    {
                    va_list arg;
                    int ordinal = 0;
                    char *item;

                    va_start(arg,Ma tch);
                    for(item=va_arg (arg, char *);item != NULL; item=va_arg(arg , char *))
                    {
                    if (strcmp(Match,i tem) == 0)
                    break;
                    else
                    ++ordinal;
                    }
                    va_end(arg);

                    return (item != NULL ? ordinal : -1 );
                    }

                    And used like...

                    #include <stdio.h>

                    int WhichString(cha r *model, ...);

                    int main(int argc, char **argv)
                    {
                    enum {left,right,for ward,back,up,do wn,halt} cmd;

                    while (--argc)
                    switch (cmd = WhichString(*++ argv,
                    "left",
                    "right",
                    "forward",
                    "back",
                    "up",
                    "down",
                    "halt",
                    NULL))
                    {
                    case left: /* "left" */
                    puts("<--");
                    break;
                    case right: /* "right" */
                    puts("-->");
                    break;
                    case forward: /* "forward" */
                    puts(" <>");
                    break;
                    case back: /* "back" */
                    puts(" ><");
                    break;
                    case up: /* "up" */
                    puts(" ^\n |");
                    break;
                    case down: /* "down" */
                    puts(" |\n v");
                    break;
                    case halt: /* "halt" */
                    puts(" @\n !");
                    break;
                    case -1:
                    default:
                    puts("*** Unknown ***");
                    }

                    return 0;
                    }



                    Comment

                    • Francine.Neary@googlemail.com

                      #25
                      Re: switch on strings

                      On Oct 1, 3:25 pm, Lew Pitcher <lpitc...@teksa vvy.comwrote:
                      On Sep 25, 2:20 am, user923005 <dcor...@connx. comwrote:On Sep 24, 11:19 pm, sinbad <sinbad.sin...@ gmail.comwrote:
                      >
                      how to implement a switch case for stringse...swit ch should happen
                      depending on the strings...
                      >
                      Suggestion: do a web search for "perfect hash".
                      >
                      >From the C FAQ:
                      >
                      20.17: Is there a way to switch on strings?
                      >
                      A: Not directly. Sometimes, it's appropriate to use a separate
                      function to map strings to integer codes, and then switch on
                      those. Otherwise, of course, you can fall back on strcmp()
                      >
                      [snip]
                      >
                      For example....
                      >
                      #include <stdarg.h>
                      #include <stdlib.h>
                      >
                      /*
                      ** string compare string Match to list of strings starting with Model
                      ** return ordinal of successful match (>=0) or -1 on no match
                      */
                      >
                      int WhichString(cha r *Match, ...)
                      {
                      va_list arg;
                      int ordinal = 0;
                      char *item;
                      >
                      va_start(arg,Ma tch);
                      for(item=va_arg (arg, char *);item != NULL; item=va_arg(arg , char *))
                      {
                      if (strcmp(Match,i tem) == 0)
                      break;
                      else
                      ++ordinal;
                      }
                      va_end(arg);
                      >
                      return (item != NULL ? ordinal : -1 );
                      >
                      }
                      >
                      And used like...
                      >
                      #include <stdio.h>
                      >
                      int WhichString(cha r *model, ...);
                      >
                      int main(int argc, char **argv)
                      {
                      enum {left,right,for ward,back,up,do wn,halt} cmd;
                      >
                      while (--argc)
                      switch (cmd = WhichString(*++ argv,
                      "left",
                      "right",
                      "forward",
                      "back",
                      "up",
                      "down",
                      "halt",
                      NULL))
                      {
                      case left: /* "left" */
                      puts("<--");
                      break;
                      case right: /* "right" */
                      puts("-->");
                      break;
                      case forward: /* "forward" */
                      puts(" <>");
                      break;
                      case back: /* "back" */
                      puts(" ><");
                      break;
                      case up: /* "up" */
                      puts(" ^\n |");
                      break;
                      case down: /* "down" */
                      puts(" |\n v");
                      break;
                      case halt: /* "halt" */
                      puts(" @\n !");
                      break;
                      case -1:
                      default:
                      puts("*** Unknown ***");
                      }
                      >
                      return 0;
                      >
                      }
                      Ouch! All those strcmps! Why not a simple hash function?

                      Comment

                      • Lew Pitcher

                        #26
                        Re: switch on strings

                        On Oct 1, 12:25 pm, Francine.Ne...@ googlemail.com wrote:
                        On Oct 1, 3:25 pm, Lew Pitcher <lpitc...@teksa vvy.comwrote:
                        >
                        >
                        >
                        On Sep 25, 2:20 am, user923005 <dcor...@connx. comwrote:On Sep 24, 11:19 pm, sinbad <sinbad.sin...@ gmail.comwrote:
                        >
                        how to implement a switch case for stringse...swit ch should happen
                        depending on the strings...
                        >
                        Suggestion: do a web search for "perfect hash".
                        >
                        From the C FAQ:
                        >
                        20.17: Is there a way to switch on strings?
                        >
                        A: Not directly. Sometimes, it's appropriate to use a separate
                        function to map strings to integer codes, and then switch on
                        those. Otherwise, of course, you can fall back on strcmp()
                        >
                        [snip]
                        >
                        For example....
                        >
                        #include <stdarg.h>
                        #include <stdlib.h>
                        >
                        /*
                        ** string compare string Match to list of strings starting with Model
                        ** return ordinal of successful match (>=0) or -1 on no match
                        */
                        >
                        int WhichString(cha r *Match, ...)
                        {
                        va_list arg;
                        int ordinal = 0;
                        char *item;
                        >
                        va_start(arg,Ma tch);
                        for(item=va_arg (arg, char *);item != NULL; item=va_arg(arg , char *))
                        {
                        if (strcmp(Match,i tem) == 0)
                        break;
                        else
                        ++ordinal;
                        }
                        va_end(arg);
                        >
                        return (item != NULL ? ordinal : -1 );
                        >
                        }
                        >
                        [snip]
                        Ouch! All those strcmps! Why not a simple hash function?
                        Sure, why not? Care to show an example of how a simple hash function
                        would be better?


                        Comment

                        • user923005

                          #27
                          Re: switch on strings

                          On Oct 1, 11:17 am, Lew Pitcher <lpitc...@teksa vvy.comwrote:
                          On Oct 1, 12:25 pm, Francine.Ne...@ googlemail.com wrote:
                          >
                          >
                          >
                          >
                          >
                          On Oct 1, 3:25 pm, Lew Pitcher <lpitc...@teksa vvy.comwrote:
                          >
                          On Sep 25, 2:20 am, user923005 <dcor...@connx. comwrote:On Sep 24, 11:19 pm, sinbad <sinbad.sin...@ gmail.comwrote:
                          >
                          how to implement a switch case for stringse...swit ch should happen
                          depending on the strings...
                          >
                          Suggestion: do a web search for "perfect hash".
                          >
                          >From the C FAQ:
                          >
                          20.17: Is there a way to switch on strings?
                          >
                          A: Not directly. Sometimes, it's appropriate to use a separate
                          function to map strings to integer codes, and then switch on
                          those. Otherwise, of course, you can fall back on strcmp()
                          >
                          [snip]
                          >
                          For example....
                          >
                          #include <stdarg.h>
                          #include <stdlib.h>
                          >
                          /*
                          ** string compare string Match to list of strings starting with Model
                          ** return ordinal of successful match (>=0) or -1 on no match
                          */
                          >
                          int WhichString(cha r *Match, ...)
                          {
                          va_list arg;
                          int ordinal = 0;
                          char *item;
                          >
                          va_start(arg,Ma tch);
                          for(item=va_arg (arg, char *);item != NULL; item=va_arg(arg , char *))
                          {
                          if (strcmp(Match,i tem) == 0)
                          break;
                          else
                          ++ordinal;
                          }
                          va_end(arg);
                          >
                          return (item != NULL ? ordinal : -1 );
                          >
                          }
                          >
                          [snip]
                          Ouch! All those strcmps! Why not a simple hash function?
                          >
                          Sure, why not? Care to show an example of how a simple hash function
                          would be better?
                          In this particular case, a hash function is not needed. Just switch
                          on the first letter.




                          Comment

                          • Flash Gordon

                            #28
                            Re: switch on strings

                            user923005 wrote, On 01/10/07 20:45:
                            On Oct 1, 11:17 am, Lew Pitcher <lpitc...@teksa vvy.comwrote:
                            >On Oct 1, 12:25 pm, Francine.Ne...@ googlemail.com wrote:
                            <snip>
                            >>Ouch! All those strcmps! Why not a simple hash function?
                            >Sure, why not? Care to show an example of how a simple hash function
                            >would be better?
                            >
                            In this particular case, a hash function is not needed. Just switch
                            on the first letter.
                            A very simple hash function which just takes the first letter as the has
                            value ;-)

                            Of course, that could give lots of collisions if the strings are changed
                            or added to.
                            --
                            Flash Gordon

                            Comment

                            Working...