The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-02-2007
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
Perl code to differentiate numeric and non-numeric input

Hi All,

Is there any code in Perl which can differentiate between numeric and non-numeric input?
  #2 (permalink)  
Old 03-03-2007
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
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.
  #3 (permalink)  
Old 03-05-2007
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
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";
}
  #4 (permalink)  
Old 03-05-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
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";
}
  #5 (permalink)  
Old 03-05-2007
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
Quote:
Originally Posted by anbu23
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";
}
i don't intend to flame you, but wish to point out a few deficiencies in your code - it doesn't consider negative numbers, and real numbers, in current form, it is identifying natural numbers, along with zero
  #6 (permalink)  
Old 03-05-2007
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
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";
}
  #7 (permalink)  
Old 03-05-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Quote:
Originally Posted by Yogesh Sawant
i don't intend to flame you, but wish to point out a few deficiencies in your code - it doesn't consider negative numbers, and real numbers, in current form, it is identifying natural numbers, along with zero
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:19 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0