Loading ...
Sorry, an error occurred while loading the content.

Re: Problem with Switch

Expand Messages
  • ohaya
    Hi, I ve found my problem. It wasn t the code, but I had some bad data. Also, FYI, I did have a use Switch , but didn t include it in the snippet. Jim
    Message 1 of 3 , Feb 10, 2011
    • 0 Attachment
      Hi,

      I've found my problem. It wasn't the code, but I had some bad data.

      Also, FYI, I did have a 'use Switch', but didn't include it in the snippet.

      Jim

      --- In perl-beginner@yahoogroups.com, "ohaya" <ohaya@...> wrote:
      >
      > Hi,
      >
      > I am really new (or, actually, it's been awhile) with Perl, and am having problems getting a 'switch' working with Perl 5.8.8.
      >
      > Basically, I'm reading in a text file, and looking for lines where the first word (space delimited) matches a string, and commenting out those lines by pre-pending a "#" to it, while outputting all other lines, verbatim.
      >
      > Here's what my loop with a switch looks like:
      >
      > while(<INFILE>)
      > {
      > $inputline = $_;
      > ($start) = ($inputline =~ /\A(.*?) /);
      >
      > if ($start == "foo") {
      > print "Found: [$start]\n";
      > }
      > switch($start) {
      >
      > case "foo" {
      > print "found 'foo'";
      > print OUTFILE "#$inputline";
      > exit;
      > }
      > else {
      > print OUTFILE "$inputline";
      > }
      > }
      >
      > I added the "if ($start == "foo")", just to test.
      >
      > The thing is, when I run the program, the "if ($start == "foo")" is finding lines, but the "case "foo"" block is never being executed, and I'm ending up with the output file looking exactly like the input file.
      >
      > Can anyone tell me what I am doing wrong?
      >
      > Thanks,
      > Jim
      >
    • Shlomi Fish
      Hi Jim, ... Please don t use Switch.pm : http://perl.net.au/wiki/Freenode_Sharp_Perl_FAQ#How_can_I_do_a_switch_statement_in_Perl.3F It s a source filter which
      Message 2 of 3 , Feb 11, 2011
      • 0 Attachment
        Hi Jim,

        On Friday 11 Feb 2011 05:38:19 ohaya wrote:
        > Hi,
        >
        > I am really new (or, actually, it's been awhile) with Perl, and am having
        > problems getting a 'switch' working with Perl 5.8.8.
        >

        Please don't use Switch.pm :

        http://perl.net.au/wiki/Freenode_Sharp_Perl_FAQ#How_can_I_do_a_switch_statement_in_Perl.3F

        It's a source filter which causes many problems, and we won't be able to help
        you further if it is still used.

        Some of the alternatives are:

        1. given/when in perl-5.10.x and above (not suitable if you still want
        compatibility for perl-5.8.x and below).

        2. Sometimes dispatch tables (implemented using hashes pointing to subroutine
        references) or iterating over an array of key/value pairs until one of them
        matches will do the trick.

        3. There are also if/elsif/else chains which are not that bad.

        Regards,

        Shlomi Fish

        --
        -----------------------------------------------------------------
        Shlomi Fish http://www.shlomifish.org/
        Stop Using MSIE - http://www.shlomifish.org/no-ie/

        Chuck Norris can make the statement "This statement is false" a true one.

        Please reply to list if it's a mailing list post - http://shlom.in/reply .
      Your message has been successfully submitted and would be delivered to recipients shortly.