Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

That's an FAQ actually, as you can find browsing the output of perldoc -q number:

How do I determine whether a scalar is a number/whole/integer/f +loat? + Assuming that you don't care about IEEE notations like "NaN" or + "Infin- ity", you probably just want to use a regular expression. + if (/\D/) { print "has nondigits\n" } if (/^\d+$/) { print "is a whole number\n" } if (/^-?\d+$/) { print "is an integer\n" } if (/^[+-]?\d+$/) { print "is a +/- integer\n" } if (/^-?\d+\.?\d*$/) { print "is a real number\n" } if (/^-?(?:\d+(?:\.\d*)?|\.\d+)$/) { print "is a decimal num +ber\n" } if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/) { print "a C float\n" } + There are also some commonly used modules for the task. Scalar +::Util (distributed with 5.8) provides access to perl's internal funct +ion "looks_like_number" for determining whether a variable looks li +ke a number. Data::Types exports functions that validate data types + using both the above and other regular expressions. Thirdly, there is + "Reg- exp::Common" which has regular expressions to match various typ +es of numbers. Those three modules are available from the CPAN. + If you're on a POSIX system, Perl supports the "POSIX::strtod" +func- tion. Its semantics are somewhat cumbersome, so here's a "getn +um" wrapper function for more convenient access. This function tak +es a string and returns the number it found, or "undef" for input th +at isn't a C float. The "is_numeric" function is a front end to "getnum +" if you just want to say, ``Is this a float?'' + sub getnum { use POSIX qw(strtod); my $str = shift; $str =~ s/^\s+//; $str =~ s/\s+$//; $! = 0; my($num, $unparsed) = strtod($str); if (($str eq '') || ($unparsed != 0) || $!) { return undef; } else { return $num; } } sub is_numeric { defined getnum($_[0]) } Or you could check out the String::Scanf module on the CPAN ins +tead. The POSIX module (part of the standard Perl distribution) provi +des the "strtod" and "strtol" for converting strings to double and long +s, respectively.

I had your same problem, too :-)

Ciao!
--bronto


The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
--John M. Dlugosz

In reply to Re: How to check Inputs if Numeric by bronto
in thread How to check Inputs if Numeric by kasmot

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2024-04-24 07:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found