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

Re^7: regex return true instead of false (precedence)

by Marshall (Canon)
on Aug 27, 2019 at 02:33 UTC ( [id://11105101]=note: print w/replies, xml ) Need Help??


in reply to Re^6: regex return true instead of false (precedence)
in thread regex return true instead of false

Well part of the point that I'm trying to make is that condition should change to make the logical sense of this easier to understand.

unless ( (!($args =~ /\-rs(\s+)(\S+)/)) && (!($args =~ /\-p(\s+)(\S+)/)) && (!($args =~ /\-P(\s+)(\S+)/)) ) {..... # I figure this is easier to understand: if ( $args =~ /\-rs(\s+)(\S+)/ #update changed unless->if or $args =~ /\-p(\s+)(\S+)/ or $args =~ /\-P(\s+)(\S+)/ ) {....
I was trained as a hardware engineer. DeMorgan's Theorem is an important part of hardware design. An AND or an OR gate is actually pretty rare. The most common gate is NAND, followed by NOR. The reason for that is that the fastest gates have a logical inversion - that is due to the way the hardware works. Inverting that output slows things down.

Changing the formulation of condition can make things easier to understand. The compiler should generate similar code for the above examples. I just think that the second is easier for a human to understand.

Replies are listed 'Best First'.
Re^8: regex return true instead of false (precedence)
by AnomalousMonk (Archbishop) on Aug 27, 2019 at 03:49 UTC
    unless ( (!($args =~ /\-rs(\s+)(\S+)/)) && (!($args =~ /\-p(\s+)(\S+)/)) && (!($args =~ /\-P(\s+)(\S+)/)) ) {..... # I figure this is easier to understand: unless ( $args =~ /\-rs(\s+)(\S+)/ or $args =~ /\-p(\s+)(\S+)/ or $args =~ /\-P(\s+)(\S+)/ ) {....

    I agree the second form is easier to understand, but it's also not equivalent to the first:

    c:\@Work\Perl\monks>perl -wMstrict -le "for my $s (qw(p X)) { printf qq{for '$s' case }; unless ( (!($s =~ /p/)) && (!($s =~ /q/)) && (!($s =~ /r/)) ) { pri +nt 'A' } unless ( $s =~ /p/ or $s =~ /q/ or $s =~ /r/ ) { pri +nt 'B' } } " for 'p' case A for 'X' case B
    My understanding of haukex's point here is that the two if/unless-statement syntaxes that ovedpo15 was worried about are exactly equivalent, and so the condition expression doesn't have to change at all.

    If I had written the original code, I think I would have written something very much along the lines you and GrandFather suggest (or like this). But if the original condition expression was correct, convoluted as it was, and did not have to change, I would say it was the better part of wisdom to just leave it alone.

    I don't think we're really very far part on this one. :)


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

      When I first encountered unless in Perl I thought it was pretty cool. However in practise I'm much more used to plain ol' if and I find unless slows down comprehension of code so I hardly ever use unless. I never use negated expressions or expressions with more than one term with unless - that is just too mind bendy.

      I'd be strongly inclined to refactor the OP's condition to be like the version I used just to make the code easier to understand and maintain. In my view it is almost always worth taking the time to simplify convoluted code. And yes, putting the alternates in the regex is a completely acceptable solution.

      Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
        ... I hardly ever use unless. I never use negated expressions or expressions with more than one term with unless ...

        That tends to be my practice. I will quite often use a statement like
            next LOOP unlessX;
        for loop control where X is a fairly simple condition expression; otherwise, I agree that unless (and his scruffy pal until) are just too mind-bendy. (I think I remember TheDamian recommending this practice in his PBP.)


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

      but it's also not equivalent to the first: Quite correct...I forgot to change the unless() to a just if(). Updated my post.

      Don't do X if A,B,C are all false <- this is the original unless statement
      this the same: Do X if any of A,B,C are true (X doesn't happen if A,B,C are all false).

      I don't think there is anything truly controversial here. I would also agree if the code has been working and this is a modification, I'd leave it alone (as we see its easy to make a mistake with all these Not's!). If the code is still in initial development, then simplifying this convoluted loop condition is something I'd seriously consider.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-25 16:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found