Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: If statement seems to ignore elsif and skips to else

by ww (Archbishop)
on Sep 25, 2017 at 18:56 UTC ( #1200062=note: print w/replies, xml ) Need Help??


in reply to If statement seems to ignore elsif and skips to else

Stop at each elsif? Only if you've set breakpoints at each one, I think... but

I suspect your first or (at Ln 7) is NOT doing what you expect. You have two alternatives at Ln 8 & 9 (where you start another or clause. Suggest you group your alternatives.

UPDATE: Miscounted paren pairs. My bad.

Warning: these observations arise from a top of head scan of your submission... but can't be taken too seriously, since we have no sample of input ... and can't (from what you've shown) be sure we know what $ThisLine may contain.

Lastly, point of personal preference (only? perhaps?): rather than constucting regexen with fixed dots (for any byte), it may make the code clearer to use quantifiers... and precompiled regexen:

my $re_start = qr/../; #match string begining with two of anything (0 +or more times each!) # but still a potential problem: would match a +null string) # so, perhaps, qr/.{1).(1)/ which will not allow + a string # (preceding the comma in your initial set of rege +xen) # with anything other than two instances of somethin +g # or another

untested, but HTH.

UPDATE: demonstrates the need to test before posting...and the consequences of failing to do so. My bad again


Spirit of the Monastery but ++$anecdote ne $data

Replies are listed 'Best First'.
Re^2: If statement seems to ignore elsif and skips to else
by Laurent_R (Canon) on Sep 25, 2017 at 19:17 UTC
    Even with breakpoints it will not stop on the false if conditions.

    The test program:

    $ cat debugger.pl use strict; use warnings; use feature 'say'; my $c = shift; if ($c == 1) { say 1; } elsif ($c == 2) { say 2; } elsif ($c == 3) { say 3; } else { say "something else"; }
    And now the execution under the debugger, with the insertion of four break points:
    $ perl -d debugger.pl 4 Loading DB routines from perl5db.pl version 1.33 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(debugger.pl:5): my $c = shift; DB<1> b 7 DB<2> b 8 DB<3> b 9 DB<4> b 10 DB<5> s main::(debugger.pl:6): if ($c == 1) { DB<5> s main::(debugger.pl:13): say "something else"; DB<5> s something else Debugged program terminated. Use q to quit or R to restart, use o inhibit_exit to avoid stopping after program termination, h q, h R or h o to get additional info.
Re^2: If statement seems to ignore elsif and skips to else
by AnomalousMonk (Bishop) on Sep 25, 2017 at 23:19 UTC
    I suspect your first or (at Ln 7) is NOT doing what you expect. You have two alternatives at Ln 8 & 9 (where you start another or clause. Suggest you group your alternatives.

    I don't understand this point. Unless the OP was updated after your post and the update is not cited (in which case edgreenberg is in line to be officially Frowned Upon), all the
        ($ThisLine =~ /..,... DEBUG: /)
    and etc. matching expressions seem to be fully parenthesized and the logic unambiguous. Can you expand on this point?

    my $re_start = qr/../; #match string begining with two of anything (0 +or more times each!) # but still a potential problem: would match a +null string) # so, perhaps, qr/.{1).(1)/ which will not allow + a string # (preceding the comma in your initial set of rege +xen) # with anything other than two instances of somethin +g # or another

    I also don't get the point about  .. matching "two of anything (0 or more times ...)" and therefore possibly matching the "null" (by which I assume you mean empty) string. The only way I can see for the regex object you give in your example to match the empty string is if it is further quantified with some quantifier such as  ? * {0} {0,} that allows zero matches. However,
        qr{ .{1}.{1} }xms
    has the same problem:

    c:\@Work\Perl\monks>perl -wMstrict -le "my $rx_dotdot = qr{ .. }xms; print 'A: match empty string' if '' =~ $rx_dotdot; ;; my $rx_counted = qr{ .{1}.{1} }xms; print 'B: match empty string: counted' if '' =~ $rx_counted; ;; print 'C: match empty string: counted, quantified' if '' =~ m{ $rx_counted? }xms; " C: match empty string: counted, quantified
    The only advantage I can see for the  .{1}.{1} variation is that it makes dot-dot more visually prominent.

    Perhaps some testing is, indeed, called for.


    Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1200062]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2020-09-22 18:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (129 votes). Check out past polls.

    Notices?