Loading ...
Sorry, an error occurred while loading the content.

27491Re: [PBML] how to validate the given number is single or double digits

Expand Messages
  • Shlomi Fish
    Dec 7, 2013
    • 0 Attachment
      Hi Prasad,

      On Sat, 7 Dec 2013 10:09:12 -0800 (PST)
      Prasad M <mrprasadkrt@...> wrote:

      > Hi,
      >  
      > how to validate the given number is single or double digits.
      >  

      See the following program:

      [QUOTE]
      #!/usr/bin/perl

      use strict;
      use warnings;

      sub validate_num
      {
      my ($n) = @_;

      return ($n =~ /\A(?:[0-9]|[1-9][0-9])\z/);
      }

      my $number = shift(@ARGV);

      print +(validate_num($number) ? "Number is OK." : "Number is not OK."), "\n";

      [/QUOTE]

      Here is a shell session with it:

      [QUOTE]
      shlomif@telaviv1:~$ perl 1-2-digits.pl 1
      Number is OK.
      shlomif@telaviv1:~$ perl 1-2-digits.pl 2
      Number is OK.
      shlomif@telaviv1:~$ perl 1-2-digits.pl 0
      Number is OK.
      shlomif@telaviv1:~$ perl 1-2-digits.pl 10
      Number is OK.
      shlomif@telaviv1:~$ perl 1-2-digits.pl 100
      Number is not OK.
      shlomif@telaviv1:~$ perl 1-2-digits.pl 231313
      Number is not OK.
      shlomif@telaviv1:~$ perl 1-2-digits.pl 09
      Number is not OK.
      [/QUOTE]

      > $ABC=55;
      >  

      You should use strict and warnings, and not use uppercase letters to start
      identifiers. See:

      http://perl-begin.org/tutorials/bad-elements/

      Also "$ABC" is a bad name for a variable.

      Regards,

      Shlomi Fish

      --
      -----------------------------------------------------------------
      Shlomi Fish http://www.shlomifish.org/
      "Humanity" - Parody of Modern Life - http://shlom.in/humanity

      He who reinvents the wheel, will understand much better how a wheel works.

      Please reply to list if it's a mailing list post - http://shlom.in/reply .
    • Show all 4 messages in this topic