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

Hi all, I'm quite new to world of Perl and have a few questions. I'm trying to write a calculator program where exactly two numbers are entered, and that they are actually numbers. I'm really hung up on how to to ensure exactly two numbers are entered! Hints and suggestions are welcome. The second part seems clear to me: unless ($value =~ /^\d+$/){ "$value contains a letter. Numbers don't have letters in them!!!\n"; die; }

2005-02-05 Edited by Arunbear: Changed title from 'Newbie', as per Monastery guidelines

Replies are listed 'Best First'.
Re: Validating numeric input
by davido (Cardinal) on Feb 05, 2005 at 05:56 UTC

    It's a little more complicated than that to validate numbers. That regex doesn't allow negative numbers, nor does it allow decimal values. It only accepts positive integers.

    For authenticating a single number, it's pretty easy to use Scalar::Util's looks_like_number() function.

    You could split on whitespace to isolate two entities, and then test each one to see that it looks like a number.


    Dave

      I noticed. I am able to select for positive integers, but thats about it! When I try to do something like its just no go! This is probably fraught with mistakes, going from memory, but I'm sure you get the point Bang line $value1=shift $value2=shift unless (($value1 >= 0) or ($value1 <= 0) or ($value2 >= 0) or ($value2 <= 0)){ die; $sum "blah blah"; print "blah blah sum";
Re: Validating numeric input
by superfrink (Curate) on Feb 05, 2005 at 07:46 UTC
Re: Validating numeric input
by K_M_McMahon (Hermit) on Feb 05, 2005 at 06:20 UTC
    If you are using values entered from <STDIN> don't forget to chomp($value) the value. This will remove a newline character (if it exists). A newline character at the end of $value in your example would cause it to fail the regex as well as the above noted problems.

      lemur:~ 731> perl -le '$_ = "1234\n"; print "You sure?" if /^\d+$/' You sure? lemur:~ 732>
Re: Validating numeric input
by cognizant (Pilgrim) on Feb 05, 2005 at 06:25 UTC
    Hi, Below code validates for exact 2 numbers.
    if ($value !~ /^\d{2}$/) { print "input contains more than two digit"; }
    If input contains negative number or so. Then
    if ($value !~ /^(-)*\d{2}$/) { print "input contains more than two digit"; }
    cheers, --c
Re: Validating numeric input
by cog (Parson) on Feb 05, 2005 at 22:49 UTC
    unless ($value =~ /^\d+$/){ "$value contains a letter. Numbers don't have letters in them!!!\n"; die; }

    Actually, just because a variable has something in it that does not match \d doesn't mean it is a letter...

Re: Validating numeric input
by zentara (Cardinal) on Feb 06, 2005 at 11:08 UTC
    A good bet for you would be Regexp::Common::number

    I'm not really a human, but I play one on earth. flash japh
Re: Validating numeric input
by m-rau (Scribe) on Feb 06, 2005 at 11:33 UTC
    There is a very good chapter "NUMBERS" in the Perl Cookbook by Tom Christiansen & Nathan Torkington. This book is a very good starting point for Perl newbies if they are are no programming beginners.
    Here are some precooked solutions (the cookbook's equivalent of just-a +dd-water meals) for most common cases. warn "has nondigits" if /\D/; warn "not a natural number" unless /^\d+$/; # rejects -3 warn "not an integer" unless /^-?\d+$/; # rejects +3 warn "not an integer" unless /^[+-]?\d+$/; warn "not a decimal number" unless /^-?\d+\.?\d*$/; # rejects .2 warn "not a decimal number" unless /^-?(?:\d+(?:\.\d*)?|\.\d+)$/; warn "not a C float" unless /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;
    Michael.
Re: Validating numeric input
by Anonymous Monk on Feb 07, 2005 at 09:50 UTC
    Well, what kind of numbers do you want to accept? Is 0.123 allowed? What about .123? -123? +123? Do you allow 1,234? And if you do, is the comma used to separate thousands, or is it a European style decimal point? Do you allow 0x123? What about floats? -1.23E-45? Perl allows underscores in certain cases, like 1_234, do you? If you do, then Perl doesn't like 12_34, what about your program?

    Perhaps the regexes in the FAQ are use of you. Perhaps Scalar::Util::looks_like_number. Perhaps Regexp::Common. Perhaps you want something that isn't covered by those three.