![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Numeric Validation | sumesh.abraham | Shell Programming and Scripting | 12 | 08-04-2008 10:10 AM |
| check input = "empty" and "numeric" | geoffry | Shell Programming and Scripting | 6 | 12-13-2007 05:12 AM |
| How to check for a valid numeric input | Vijayakumarpc | Shell Programming and Scripting | 1 | 08-04-2007 08:34 AM |
| Checking numeric value | malaymaru | Shell Programming and Scripting | 6 | 05-25-2007 08:42 PM |
| non-numeric argument | TiredOrangeCat | UNIX for Dummies Questions & Answers | 3 | 02-13-2007 02:37 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
The most common way:
Use regular expression. Alternative (probably a better) way: Code:
cbkihong@cbkihong:~$ perl -MScalar::Util -e
'print(Scalar::Util::looks_like_number("-8.90e10")?"Looks like a number.\n":"Not a number.\n")'
Looks like a number.
cbkihong@cbkihong:~$ perl -MScalar::Util -e
'print(Scalar::Util::looks_like_number("89d")?"Looks like a number.\n":"Not a number.\n")'
Not a number.
|
|
||||
|
Hi ,
Below is my Perl code using regular expression. but it doesn;t seem to work. Can anybody tell me what's wrong with my code ? Code:
#!/usr/bin/perl
print "Enter a term: ";
$Input = <STDIN>;
print "\n";
if ($input == [0-9]) {
print "$Input is numeric\n";
}
else {
print "$Input is non-numeric\n";
}
|
|
|||||
|
this one identifies integers, real numbers, and negative numbers as well:
Code:
#!/usr/bin/perl
use strict;
print "Enter a number: ";
chomp (my $num = <STDIN>);
if ($num =~ m/^-?\d+$/) {
print "An integer \n";
}
elsif ($num =~ m/^-?\d+[\/|\.]\d+$/) {
print "real number! \n";
}
else {
print "not a number \n";
}
|
|
||||
|
Quote:
Code:
#!/usr/bin/perl
print "Enter a term: ";
$input = <STDIN>;
print "\n";
if ( $input =~ /^[\+-]*[0-9]*\.*[0-9]*$/ && $input !~ /^[\. ]*$/ ) {
print "$Input is numeric\n";
}
else {
print "$Input is non-numeric\n";
}
Last edited by anbu23; 03-05-2007 at 04:57 AM.. |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|