Loading ...
Sorry, an error occurred while loading the content.
 

Re: [PBML] Is this scaler a number?

Expand Messages
  • the_land_baron
    Thanks. A regexp did the trick for me. Simpler than using other modules. -Jerramy ... number/whole/integer/float? ... or ... whether a ... other ... numbers.
    Message 1 of 5 , Apr 21, 2005
      Thanks.

      A regexp did the trick for me. Simpler than using other modules.

      -Jerramy


      --- In perl-beginner@yahoogroups.com, ateeq <ateeq@s...> wrote:
      > For me this $perldoc -q number
      >
      > gives thisoutput too...
      >
      > How do I determine whether a scalar is a
      number/whole/integer/float?
      >
      > Assuming that you don't care about IEEE notations like "NaN"
      or
      > "Infinity", 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
      > number\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 function "looks_like_number" for determining
      whether a
      > variable looks like a number. Data::Types exports func-
      > tions that validate data types using both the above and
      other
      > regular expressions. Thirdly, there is "Regexp::Common"
      > which has regular expressions to match various types of
      numbers.
      > Those three modules are available from the CPAN.
      >
      > If you're on a POSIX system, Perl supports
      the "POSIX::strtod"
      > function. Its semantics are somewhat cumbersome, so
      > here's a "getnum" wrapper function for more convenient
      access.
      > This function takes a string and returns the number it
      > found, or "undef" for input that 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
      > instead. The POSIX module (part of the standard Perl distri-
      > bution) provides the "strtod" and "strtol" for converting
      strings
      > to double and longs, respectively.
      > --------------------------------------------------------------------
      ---------------------------------------------------
      >
      > J.E. Cripps wrote:
      >
      > >
      > >
      > >
      > >>Is there a simple way to determine if a scaler is a number?
      > >>
      > >>
      > >
      > >ITYM "scalar"
      > >
      > >
      > >
      > >>These are okay:
      > >>$scaler = '0'
      > >>$scaler = '234'
      > >>$scaler = '-23.25'
      > >>$scaler = '+1.2345e-4'
      > >>
      > >>
      > >
      > > % perldoc -q number
      > >
      > >lead me to :
      > >
      > > Found in /usr/local/lib/perl5/5.8.5/pod/perlfaq4.pod
      > > How do I determine whether a scalar is a
      number/whole/integer/float?
      > >
      > > Assuming that you don't care about IEEE notations
      like "NaN" or "Infin-
      > > ity", you probably just want to use a regular expression.
      > >
      > >There's a regexp for the scientific notation there.
      > >
      > >
      > >TMTOWDI:
      > >
      > > There are also some commonly used modules for the task.
      > >
      > >
      > >
      > >
      > >Unsubscribing info is here:
      http://help.yahoo.com/help/us/groups/groups-32.html
      > >Yahoo! Groups Links
      > >
      > >
      > >
      > >
      > >
      > >
      > >
      > >
      > >
      >
      >
      >
      > [Non-text portions of this message have been removed]
    Your message has been successfully submitted and would be delivered to recipients shortly.