newby2perl has asked for the wisdom of the Perl Monks concerning the following question:

want to check a field for content...it should contain only numeric values like 12345.67 or 0 or 0.00 or -123.78 here is code I am struggling with

while ( $amount2 =~ / / ){ $amount2 =~ s/ //g; } if ( ($amount2) =~ /_(\d+)\./) { print BAD_OUTPUT "Amount field cannot contain alpha characters :"; + print BAD_OUTPUT $_,"\n"; next; }

Replies are listed 'Best First'.
Re: only want numerics in field
by davido (Cardinal) on Apr 21, 2014 at 16:37 UTC

    You could use Regexp::Common's "number" extension to detect reals, which is what you seem to be seeking:

    if( $string =~ /^$RE{num}{real}$/ ) { print "Match!\n"; } else { print "Reject!\n"; }

    You don't want to invent this yourself. Just look at the regular expression that $RE{num}{real} contains:

    perl -MRegexp::Common=number -E 'say $RE{num}{real}' (?:(?i)(?:[-+]?)(?:(?=[.]?[0123456789])(?:[0123456789]*)(?:(?:[.])(?:[ +0123456789]{0,}))?)(?:(?:[E])(?:(?:[-+]?)(?:[0123456789]+))|))

    ...better off using a solution from Regexp::Common, or the looks_like_number() utility Limbic~Region suggested, or both.


    Dave

Re: only want numerics in field
by Limbic~Region (Chancellor) on Apr 21, 2014 at 15:44 UTC
    newby2perl,
    Scalar::Util has a function call looks_like_number(). The only thing is I believe it allows scientific notation which your example doesn't include.

    Cheers - L~R

Re: only want numerics in field
by bigj (Monk) on Apr 21, 2014 at 15:52 UTC
    Your formatting is pretty much screwed. However, if you want to check a field for content...it should contain only numeric values like 12345.67 or 0 or 0.00 or -123.78, the usual regexp to do that would be like:
    /^[+-]\d+(?:\.\d+)?$/ # or if you want either no decimals or exactly 2 decimals /^[+-]\d+(?:\.\d\d)?$/
    But I think, if I look to your code, you seem to have more than just a decimal number inside. You might also take a look to modules like Regexp::Common. Most of us will make a mistake here or their when build Regexps, the good thing common things is that we aren't the first one doing them :-)

    Greetings,
    Janek Schleicher

      bigj,
      Your regex will indicate that .25 is not a number. You also require a leading sign [+-]. I realize that defintion of "what is numeric" is sorely lacking by the OP but I wanted to point this out to anyone who may come along later.

      Cheers - L~R

        Just one more reason to not reinvent the wheel :-) You're of course right.

        Greetings,
        Janek Schleicher

      yes it will be an amount with only 2 places after the decimal.

        it should allow - for negative numbers

Re: only want numerics in field
by AnomalousMonk (Archbishop) on Apr 21, 2014 at 19:12 UTC

    BTW, a tangential point: The loop

    while ( $amount2 =~ / / ){ $amount2 =~ s/ //g; }
    conditionally does a substitution to remove all spaces if a space is found in a string. However,  s/ //g on its own will never do anything if a match is not found. You are essentially doing one match for the price of two. You can profitably lose the loop (but take the substitution).