in reply to How to check Inputs if Numeric

Here is a possibility. It checks whether anything in your input contains something else than digits or decimal point.
It fails with strings like '....' or '1.2.333.44..55' :
#!/usr/bin/perl -l use strict; my @inputs = ( qw[1 2 878787 777s7 3.4 3,4 77 abcdef ABCSDEF :: איט !! +!?]); foreach (@inputs) { if ( m/[^0-9.]/ ) {print "$_ \tis not numeric";} else {print "$_ \tis numeric";} } __OUTPUT__ 1 is numeric 2 is numeric 878787 is numeric 777s7 is not numeric 3.4 is numeric 3,4 is not numeric 77 is numeric abcdef is not numeric ABCSDEF is not numeric :: is not numeric איט is not numeric !!!? is not numeric __END__

pelagic
-------------------------------------
I can resist anything but temptation.

Replies are listed 'Best First'.
Re: Re: How to check Inputs if Numeric
by davis (Vicar) on Mar 31, 2004 at 14:52 UTC

    Yours fails for negative numbers, and for strings with more than one decimal place


    davis
    It's not easy to juggle a pregnant wife and a troubled child, but somehow I managed to fit in eight hours of TV a day.
      > Yours fails for negative numbers
      I know it's primitive. But I coded it in about 5 seconds and for some application it might be good enough.

      > , and for strings with more than one decimal place
      Not True. It declares anything containing something else than numbers and decimal ponit(s) as non-numeric.

      For a real application I'd use something like Abigail's Regexp::Common

      pelagic
      -------------------------------------
      I can resist anything but temptation.