Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Match all Non-0 and Letters

by Laurent_R (Canon)
on Jun 24, 2017 at 09:11 UTC ( [id://1193439]=note: print w/replies, xml ) Need Help??


in reply to Match all Non-0 and Letters

Hi arblargan,

as other monks have already mentioned, all you really need is a single regex such as /0{7}\d/ (or perhaps /^0{7}\d$/ if the word you get is just the number).

You could, however, split your word into two parts as you did, but you made a logical error: you should have a "or", not an "and" in your condition for detecting a corrupt word, because you want to detect if the first part is not made of 0 OR if the second part is not a digit. So, you might fix your code as follows:

my $Disc = get_word(); my $D1 = substr($Disc,0,7); my $D2 = substr($Disc,7,1); print "Word $Disc is corrupt!\n" if $D1 !~ /0+/ or $D2 !~ /[0-9]+/;
But, again, this was just to explain the error in your code, the solution with /0{7}\d/ is much simpler and better.

Update: this was intended to show the logical error ("and" instead of "or"). As pointed out by AnomalousMonk just below, the regexes are also wrong in terms of the intended purpose described in the original post.

Replies are listed 'Best First'.
Re^2: Match all Non-0 and Letters
by AnomalousMonk (Archbishop) on Jun 24, 2017 at 17:34 UTC
    ... this was just to explain the error in your code ... /0{7}\d/ is much simpler and better.

    I understand that the intended purpose of the code example is very limited, but I think it's very important to point out that the
        $D1 !~ /0+/
    test ("if there is not at least one '0' in the first 7 digits") is also a fundamental error.


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

      Yes, AnomalousMonk, you're right. I wanted to point out the logical error ("and" instead of "or" in the conditional), but you're absolutely right that the regexes should also be fixed.

      Perhaps something like:

      print "Word $Disc is corrupt!\n" if $D1 !~ /^0{7}$/ or $D2 !~ /[0-9]/;
      And the first part of the conditional could actually be replaced by a string inequality operator rather than a regex:
      print "Word $Disc is corrupt!\n" if ($D1 ne '0' x 7) or $D2 !~ /[0-9]/ +;
      Update: s/instead or "or"/instead of "or"/;. Thanks to Discipulus for pointing out the typo.
        ... the first part of the conditional could actually be replaced by a string inequality ...

        That's what I was thinking, but my idea was  $D1 ne '0000000' :)


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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-20 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found