http://www.perlmonks.org?node_id=857993

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

$val=10.2455; validate(2,2,$val); sub validate{ $v1=$_[0]; $v2=$_[1]; $v3=$_[2]; if($v3= ~m /\d{2}?\.\d{2}?/ ){ print "its a good one\n";} else {print "Not Good!!";}

Replies are listed 'Best First'.
Re: floating point validation
by moritz (Cardinal) on Aug 30, 2010 at 12:04 UTC
    From skimming the CB it seems that the original problem (whatever it was) was solved, and this is the solution.

    The question seemed to be how to validate a given string against a number format with given number of decimals before and after the decimal point.

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: floating point validation
by ikegami (Patriarch) on Aug 30, 2010 at 14:03 UTC
Re: floating point validation
by govindkailas (Acolyte) on Aug 30, 2010 at 12:06 UTC
    here is the corrected one :) thanks to all ..
    $val=101.24; validate(3,2,$val); sub validate{ $v1=$_[0]; $v2=$_[1]; $v3=$_[2]; print "Values are $v1 \t $v2 \t $v3 \n"; if($v3=~m/^\d{$v1}\.\d{$v2}$/ ){ print "its a good one\n";} else {print "Not Good!!";} }
      I tidied the script for you:).
      #!/usr/bin/perl use strict; use warnings; my $val = 101.24; validate( 3, 2, $val ); sub validate { my $v1 = $_[0]; my $v2 = $_[1]; my $v3 = $_[2]; print "Values are $v1 \t $v2 \t $v3 \n"; if ( $v3 =~ m/^\d{$v1}\.\d{$v2}$/ ) { print "its a good one\n"; } else { print "Not Good!!"; } }

        I tidied the tidied.

        my $val = 101.24; print match_decimal_format( 3, 2, $val ) ? "Valid!\n" : "Invalid...\n" +; sub match_decimal_format { my ( $leading, $trailing, $number ) = @_; return $number =~ m/^ [0-9]{$leading} \. [0-9]{$trailing} $/x; }
Re: floating point validation
by ww (Archbishop) on Aug 30, 2010 at 12:46 UTC
    When posting, please include an explicit question. Your OP can be interpreted in many ways; most of them utterly unrelated to your actual intent.
Re: floating point validation
by trwww (Priest) on Aug 30, 2010 at 17:57 UTC
    use Regexp::Common; if ( $RE{num}{real}->matches($v3) ) { print "its a good one\n"; } else { print "Not Good!!"; }

    Regexp::Common is one of the best libraries on CPAN!

Re: floating point validation
by sundialsvc4 (Abbot) on Aug 30, 2010 at 21:53 UTC

    It is also worth mentioning that it’s frequently simplest to just attempt to convert the string input to a number ... and to catch any exception that might be thrown.   Instead of testing the string each time to see if it “looks like a number,” you simply assume that it does, and catch the (rare) case where it turns out that it doesn’t.

Re: floating point validation
by aquarium (Curate) on Aug 31, 2010 at 00:40 UTC
    to get something more generic that is further extensible or easily changed, write a function that produces a (contrived) format of the input as its output. then you can easily change the truth conditional to fit purposes of the day.
    first check that it's a number by adding zero and testing it still equals original input/number. then decide whether you need signed or not, with or without thousands separator, scientific notation, etc. i assume most of these can be excluded, apart from "not a number" result. then convert all digits and the decimal point to something like d->5->2 or similar, this return value scheme meaning the result is a number (the d), with 5 digits, and decimal point (if any) after second digit. a "not a number" result encoding could be something like "x" or "a" instead of "d". anyway, some form of easily parsable return code of this or similar kind is then easily tested for any preference of conditional, e.g. only want d->4->2 as per the example presented. hope it makes sense.
    the hardest line to type correctly is: stty erase ^H