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

matching digits and characters

by Anonymous Monk
on Aug 22, 2003 at 16:13 UTC ( #285842=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to make sure that $str contains only numbers using the following:
my $match = '[^\D]'; print "non digits found" if $str !~ /$match/g;
there are about 10 $match's, set based on another condition. This is the only one that I have been unable to get working.

Comment on matching digits and characters
Download Code
Re: matching digits and characters
by chunlou (Curate) on Aug 22, 2003 at 16:21 UTC
    Perhaps it's easier to use Regexp::Common. It takes care of various numeric formats for you as well (if that's what you also want).
    use Regexp::Common; $str =~ /$RE{num}{real}/;
Re: matching digits and characters
by BrowserUk (Pope) on Aug 22, 2003 at 16:26 UTC

    Wow! That's the first triple negative I've seen in the wild in a long time:)

    You code reads:

    Print "non-digits found" if $str doesn't match things that are not non-digits!

    Try: print '"non digits found" if $str contains non-digits. (It's easier for a human being to read:)

    print 'non digits found' if $str =~ /\D/;

    Note: Using /g in the scalar context of an if statement is probably superfluous.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.

      Using /g in the scalar context of an if statement is probably superfluous.

      s/superfluous/a mistake/. //g in a scalar context can make your match fail every so often. I've seen this type of bug reported here several times.

      This particular code, in isolation, probably wouldn't notice the bug (perhaps that is what you meant) since $str appears to be getting updated at least once per regex application. But the use of //g in a scalar context is something that should be vigorously avoided unless you need that advanced magic and understand it.

      I've seen this mistake enough that I'd support a pragma that makes this illegal unless you ask for it. But, in practice, I don't see how to make such useful.

                      - tye

        Yes, I probably should have stated it more strongly.

        I have been bitten by this a few times which is why it stood out at me. I didn't have enough confidence to say the it was always, or even nearly always a mistake without thinking it through further. I know that I have used it to good effect at least once, hence the prevarication.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
        If I understand your problem, I can solve it! Of course, the same can be said for you.

Re: matching digits and characters
by davido (Archbishop) on Aug 22, 2003 at 17:31 UTC
    First, there's no need for a triple negative. That's like saying:

    my $$$a = \\$b;

    just because you can.

    And there's no need to put \D in a character class. It is, itself, a character class. Only put it in a character class if you intend to also add other things to it.

    And though /g is useful in many situations (even in Scalar context), it's not helping you here because you're simply looking for a nondigit somewhere in the string. Regular expressions look through the whole string automatically, for one instance of a match, and return true as soon as they find that instance. If you had a situation where you cared about multiple matches, you would use /g, but you only want to reject the string if it contains one or more. You don't really care about the "or more" part. One is enough to be a problem, so just get rid of the /g; it wasn't working for you in that example anyway.

    So after all's said and done, your code should look like this:

    my $match = '\D'; print "non digits found\n" if $str =~ $match;

    Hope this helps!

    Dave

    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://285842]
Approved by Mr. Muskrat
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2014-04-25 09:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (586 votes), past polls