Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regexp issue 3

Status
Not open for further replies.

JohnLucania

Programmer
Oct 10, 2005
96
US
I am seeing two issues:

1) the 1st entry doesn't get any regexp checked.
Enter Number: 100

2) regexps are not matching correctly.

Enter Number or 'exit' to exit: 100
100 is a whole number
100 is an integer
100 is a +/- integer
100 is a real number
100 is a decimal number
100 a C float
Enter Number or 'exit' to exit: 1.3
1.3 has nondigits
1.3 is a real number
1.3 is a decimal number
1.3 a C float
Enter Number or 'exit' to exit: -110.02
-110.02 has nondigits
-110.02 is a real number
-110.02 is a decimal number
-110.02 a C float

why is that?

#! /usr/bin/perl

use warnings;
use strict;

my $YourNumber;

print "Enter Number: ";
chomp($YourNumber = <STDIN>);

while ($YourNumber ne 'exit') {
print "Enter Number or 'exit' to exit: ";
chomp($YourNumber = <STDIN>);

if ( $YourNumber eq 'exit') {
print "Bye Bye!";
exit 1;
}
if ($YourNumber =~ /\D/)
{ print "$YourNumber has nondigits\n" };
if ($YourNumber =~ /^\d+$/)
{ print "$YourNumber is a whole number\n" };
if ($YourNumber =~ /^-?\d+$/)
{ print "$YourNumber is an integer\n" };
if ($YourNumber =~ /^[+-]?\d+$/)
{ print "$YourNumber is a +/- integer\n" };
if ($YourNumber =~ /^-?\d+\.?\d*$/)
{ print "$YourNumber is a real number\n" };
if ($YourNumber =~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/)
{ print "$YourNumber is a decimal number\n" };
if ($YourNumber =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/)
{ print "$YourNumber a C float\n" };
}
 
For #1, it prompts for a number just before the loop, and prompts again as soon as it enters the loop. The easiest fix is probably to remove the prompt outside of the loop.

For #2, just a thought: with a proper regex, wouldn't the "whole number", "integer" and "+/- integer" tests be the same thing?
 
a bit more readable...

Code:
[b]#!/usr/bin/perl[/b]

use warnings;
use strict;

my $YourNumber;

while ($YourNumber ne 'exit') {

  print "Enter Number or 'exit' to exit: ";
  chomp($YourNumber = <STDIN>);

  if ( $YourNumber eq 'exit' ) {
    print "Bye Bye!";
    exit 1;
  }
  
  print "$YourNumber has nondigits\n"       if $YourNumber =~ /\D/;
  print "$YourNumber is a whole number\n"   if $YourNumber =~ /^\d+$/;
  print "$YourNumber is an integer\n"       if $YourNumber =~ /^-?\d+$/;
  print "$YourNumber is a +/- integer\n"    if $YourNumber =~ /^[+-]?\d+$/;
  print "$YourNumber is a real number\n"    if $YourNumber =~ /^-?\d+\.?\d*$/;
  print "$YourNumber is a decimal number\n" if $YourNumber =~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/;
  print "$YourNumber a C float\n"           if $YourNumber =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;
             
}


Kind Regards
Duncan
 
/\D/ - Of course "-" and "." are nondigits
/^\d+$/ - It wouldn't catch -100 - it is whole number also
/^-?\d+\.?\d*$/ - You should remove "?" after the dot
I think that in the next two the problem is similar, I haven't watched them closely.
When you have real, you SHOULD have "."




Corwin
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top