sivasakthi schreef:

> I have tried to find the entered number is single digit or not..
> 
> the code is following,
> 
> #! /usrbin/perl

That shebang line looks a bit strange.


> use strict;
> use warnings;
> 
> my $tmp=<STDIN>;

Whitespace is cheap:

  my $tmp = <STDIN>;


> if($tmp =~ m/(\d)/)

You need to (add whitespace and) anchor the regex:

  if ($tmp =~ m/^(\d)$/)

or even: if ($tmp =~ m/\A\s*(\d)\s+\z/)


> {
> print"NO IS SINGLE DIGIT\n";

More whitespace issues: indent the line, add one after the print:

       print "NO IS SINGLE DIGIT\n";

> }
> else
> {
> print"NO IS NOT SINGLE DIGIT\n";
> }
> 
> 
> 
> but it always passed the condition even if we entered the more no of
> digit numbers??

Anchoring the regex was your main issue. Lack of whitespace too. ;)


#!/usr/bin/perl
  use strict;
  use warnings;

  while (1) {
      print "\nEnter some text: ";
      $_ = <STDIN>;
      last if /^$/;

      print "Yes, that was single digit: $1.\n" and next
          if /^(\d)$/;

      print "No, that was not a single digit.\n";
  }

__END__


-- 
Affijn, Ruud

"Gewoon is een tijger."

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to