## how to validate the given number is single or double digits

Expand Messages
• Hi,   how to validate the given number is single or double digits.   \$ABC=55;     thanks, Prasad Hi, how to validate the given number is single or double
Message 1 of 4 , Dec 7, 2013
Hi,

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

\$ABC=55;

thanks,
• Hi Prasad, On Sat, 7 Dec 2013 10:09:12 -0800 (PST) ... See the following program: [QUOTE] #!/usr/bin/perl use strict; use warnings; sub validate_num { my (\$n)
Message 2 of 4 , Dec 7, 2013

On Sat, 7 Dec 2013 10:09:12 -0800 (PST)

> 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:

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.

• You can use lengh() function for count the chars in any variable. Regards, Farahim Khan  ... From: Prasad M Date:08/12/2013 4:36 AM
Message 3 of 4 , Dec 8, 2013
You can use lengh() function for count the chars in any variable.

Regards,
Farahim Khan

-------- Original message --------
Date:08/12/2013 4:36 AM (GMT+05:30)
To: perl-beginner@yahoogroups.com
Subject: [PBML] how to validate the given number is single or double digits

Hi,

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

\$ABC=55;

thanks,

• This being Perl, there s more than one way to do it. This is a slightly more mathematical approach than Shlomi s regex-based method: sub digits { my (\$n) = @_;
Message 4 of 4 , Dec 18, 2013
This being Perl, there's more than one way to do it.

This is a slightly more mathematical approach than Shlomi's regex-based method:

sub digits {
my (\$n) = @_;
return \$n == 0 ? 1 : int( log(abs(\$n)) / log(10) ) + 1;
}

sub valid {
my (\$n) = @_;
return ( digits(\$n) <= 2 );  # returns 1 if true, undef if false
}

Both approaches have their pros and cons.  Using a regular expression will detect if the input is not a number.   Taking the logarithm will tell you how many digits and may be faster.   Chose the one that's appropriate to your task.

On 12/08/2013 01:31 AM, Shlomi Fish wrote:

On Sat, 7 Dec 2013 10:09:12 -0800 (PST)

> 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:

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.