## RE: [PBML] How to check if the user input is number?

Expand Messages
• Here is a section from the perl cookbook. ... Checking Whether a String Is a Valid Number Problem You want to check whether a string represents a valid number.
Message 1 of 11 , Apr 2 3:05 PM
Here is a section from the perl cookbook.
-------------------------------------------------------------
Checking Whether a String Is a Valid Number

Problem

You want to check whether a string represents a valid number. This is a common problem when validating input, as in a CGI script.

Solution

Compare it against a regular expression that matches the kinds of numbers you're interested in.

if (\$string =~ /PATTERN/) {
# is a number
} else {
# is not
}

Discussion

This problem gets to the heart of what we mean by a number. Even things that sound simple, like integer, make you think hard about what you will accept ("Is a leading + for
positive numbers optional, mandatory, or forbidden?"). The many ways that floating-point numbers can be represented could overheat your brain.

You must decide what you will and will not accept. Then, construct a regular expression to match those things alone. Here are some precooked solutions (the cookbook's
equivalent of just-add-water meals) for most common cases.

warn "has nondigits" if /\D/;
warn "not a natural number" unless /^\d+\$/; # rejects -3
warn "not an integer" unless /^-?\d+\$/; # rejects +3
warn "not an integer" unless /^[+-]?\d+\$/;
warn "not a decimal number" unless /^-?\d+\.?\d*\$/; # rejects .2
warn "not a decimal number" unless /^-?(?:\d+(?:\.\d*)?|\.\d+)\$/;
warn "not a C float"
unless /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?\$/;

-----Original Message-----
From: Jaco [mailto:jaco2001uk@...]
Sent: Wednesday, April 02, 2003 14:41
To: perl-beginner@yahoogroups.com
Subject: [PBML] How to check if the user input is number?

Hi

Hope this isn't too silly a question but I am very new to Perl and am trying
to check if the users input is a number.

Basically, here's what I've got so far. (I know it's probably not very good
but I'm learning!)

chomp (\$times_table = <STDIN>);

if (\$times_tables =~ [0-9])
{
print "\nOK, so you want to try your \$times_table times tables!\n";
}
else
{
exit;
}

What I want to do is
if \$times_table is a number then continue through the program, if it's not
exit

Any help would be really appreciated

TIA

David

<http://rd.yahoo.com/M=246920.2960106.4328965.1728375/D=egroupweb/S=1705006951:HM/A=1513704/R=0/*http://www.gotomypc.com/u/tr/yh/cpm/grp/300_02F/g22lp?Target=mm/g22lp.tmpl>

Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.html

[Non-text portions of this message have been removed]
• Thanks everyone for your help :) I ve read a few basic things on regexps but need to find loads of examples really to read through and try and understand To
Message 2 of 11 , Apr 2 3:43 PM
Thanks everyone for your help :)

I've read a few basic things on regexps but need to find loads of examples
really to read through and try and understand

To elaborate a bit: Before the code posted earlier by me, I have this:

for (\$i = 1; \$i <= 10; \$i++)
{
print "[\$i] \$i times tables.\n";
}
print "\n";

chomp (\$times_table = <STDIN>);

etc ....

It's a program for my son to practice his times tables and prints out a list
times tables, dependant on the upper/max value of \$i above.

Is there some way that I could incorporate that into the regexp?
For example, say he got really good at his times tables and wanted to be
able to do up to his 25 times tables

We now have
(\$i = 1; \$i <= 25; \$i++)

At the moment if I type in q then it tries getting me to do my "q" times
tables !
So basically if it's not a value that I takes then the program should
die/exit.
If it is then it continues.

The question is how could the regexp check to see if the value belonged to
all possible values that \$i takes ???

(Hope that makes sense!)

Cheers

David

-----Original Message-----
From: Stern, Leon [mailto:lstern@...]
Sent: 03 April 2003 00:05
To: perl-beginner@yahoogroups.com
Subject: RE: [PBML] How to check if the user input is number?

Here is a section from the perl cookbook.
-------------------------------------------------------------
Checking Whether a String Is a Valid Number

Problem

You want to check whether a string represents a valid number. This is a
common problem when validating input, as in a CGI script.

Solution

Compare it against a regular expression that matches the kinds of numbers
you're interested in.

if (\$string =~ /PATTERN/) {
# is a number
} else {
# is not
}

Discussion

This problem gets to the heart of what we mean by a number. Even things that
sound simple, like integer, make you think hard about what you will accept
positive numbers optional, mandatory, or forbidden?"). The many ways that
floating-point numbers can be represented could overheat your brain.

You must decide what you will and will not accept. Then, construct a regular
expression to match those things alone. Here are some precooked solutions
(the cookbook's
equivalent of just-add-water meals) for most common cases.

warn "has nondigits" if /\D/;
warn "not a natural number" unless /^\d+\$/; # rejects -3
warn "not an integer" unless /^-?\d+\$/; # rejects +3
warn "not an integer" unless /^[+-]?\d+\$/;
warn "not a decimal number" unless /^-?\d+\.?\d*\$/; # rejects .2
warn "not a decimal number" unless /^-?(?:\d+(?:\.\d*)?|\.\d+)\$/;
warn "not a C float"
unless /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?\$/;

-----Original Message-----
From: Jaco [mailto:jaco2001uk@...]
Sent: Wednesday, April 02, 2003 14:41
To: perl-beginner@yahoogroups.com
Subject: [PBML] How to check if the user input is number?

Hi

Hope this isn't too silly a question but I am very new to Perl and am trying
to check if the users input is a number.

Basically, here's what I've got so far. (I know it's probably not very good
but I'm learning!)

chomp (\$times_table = <STDIN>);

if (\$times_tables =~ [0-9])
{
print "\nOK, so you want to try your \$times_table times tables!\n";
}
else
{
exit;
}

What I want to do is
if \$times_table is a number then continue through the program, if it's not
exit

Any help would be really appreciated

TIA

David

<http://rd.yahoo.com/M=246920.2960106.4328965.1728375/D=egroupweb/S=17050069
51:HM/A=1513704/R=0/*http://www.gotomypc.com/u/tr/yh/cpm/grp/300_02F/g22lp?T
arget=mm/g22lp.tmpl>

l/S=:HM/A=1513704/rand=421178574>

Unsubscribing info is here:
http://help.yahoo.com/help/us/groups/groups-32.html

<http://docs.yahoo.com/info/terms/> .

[Non-text portions of this message have been removed]

Unsubscribing info is here:
http://help.yahoo.com/help/us/groups/groups-32.html

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
• You need something like this untested code -- \$flag = keep-going ; while ( \$flag eq keep-going ) { print Enter the table you want to work with ;
Message 3 of 11 , Apr 2 3:49 PM
You need something like this untested code --

\$flag = "keep-going";

while ( \$flag eq "keep-going") {
print "Enter the table you want to work with <1-25> ";

\$value = <>;
chomp \$value;
if ( \$value > 0 && \$value < 26 ) {
\$flag = "exit-loop";
}
else {
print "\$value is not a number between 1 and 25\n";
}
}

Leon

-----Original Message-----
From: Jaco [mailto:jaco2001uk@...]
Sent: Wednesday, April 02, 2003 15:43
To: perl-beginner@yahoogroups.com
Subject: RE: [PBML] How to check if the user input is number?

Thanks everyone for your help :)

I've read a few basic things on regexps but need to find loads of examples
really to read through and try and understand

To elaborate a bit: Before the code posted earlier by me, I have this:

for (\$i = 1; \$i <= 10; \$i++)
{
print "[\$i] \$i times tables.\n";
}
print "\n";

chomp (\$times_table = <STDIN>);

etc ....

It's a program for my son to practice his times tables and prints out a list
times tables, dependant on the upper/max value of \$i above.

Is there some way that I could incorporate that into the regexp?
For example, say he got really good at his times tables and wanted to be
able to do up to his 25 times tables

We now have
(\$i = 1; \$i <= 25; \$i++)

At the moment if I type in q then it tries getting me to do my "q" times
tables !
So basically if it's not a value that I takes then the program should
die/exit.
If it is then it continues.

The question is how could the regexp check to see if the value belonged to
all possible values that \$i takes ???

(Hope that makes sense!)

Cheers

David

-----Original Message-----
From: Stern, Leon [mailto:lstern@...]
Sent: 03 April 2003 00:05
To: perl-beginner@yahoogroups.com
Subject: RE: [PBML] How to check if the user input is number?

Here is a section from the perl cookbook.
-------------------------------------------------------------
Checking Whether a String Is a Valid Number

Problem

You want to check whether a string represents a valid number. This is a
common problem when validating input, as in a CGI script.

Solution

Compare it against a regular expression that matches the kinds of numbers
you're interested in.

if (\$string =~ /PATTERN/) {
# is a number
} else {
# is not
}

Discussion

This problem gets to the heart of what we mean by a number. Even things that
sound simple, like integer, make you think hard about what you will accept
positive numbers optional, mandatory, or forbidden?"). The many ways that
floating-point numbers can be represented could overheat your brain.

You must decide what you will and will not accept. Then, construct a regular
expression to match those things alone. Here are some precooked solutions
(the cookbook's
equivalent of just-add-water meals) for most common cases.

warn "has nondigits" if /\D/;
warn "not a natural number" unless /^\d+\$/; # rejects -3
warn "not an integer" unless /^-?\d+\$/; # rejects +3
warn "not an integer" unless /^[+-]?\d+\$/;
warn "not a decimal number" unless /^-?\d+\.?\d*\$/; # rejects .2
warn "not a decimal number" unless /^-?(?:\d+(?:\.\d*)?|\.\d+)\$/;
warn "not a C float"
unless /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?\$/;

-----Original Message-----
From: Jaco [mailto:jaco2001uk@...]
Sent: Wednesday, April 02, 2003 14:41
To: perl-beginner@yahoogroups.com
Subject: [PBML] How to check if the user input is number?

Hi

Hope this isn't too silly a question but I am very new to Perl and am trying
to check if the users input is a number.

Basically, here's what I've got so far. (I know it's probably not very good
but I'm learning!)

chomp (\$times_table = <STDIN>);

if (\$times_tables =~ [0-9])
{
print "\nOK, so you want to try your \$times_table times tables!\n";
}
else
{
exit;
}

What I want to do is
if \$times_table is a number then continue through the program, if it's not
exit

Any help would be really appreciated

TIA

David

< http://rd.yahoo.com/M=246920.2960106.4328965.1728375/D=egroupweb/S=17050069
51:HM/A=1513704/R=0/* http://www.gotomypc.com/u/tr/yh/cpm/grp/300_02F/g22lp?T
arget=mm/g22lp.tmpl>

l/S=:HM/A=1513704/rand=421178574>

Unsubscribing info is here:
http://help.yahoo.com/help/us/groups/groups-32.html

< http://docs.yahoo.com/info/terms/> .

[Non-text portions of this message have been removed]

Unsubscribing info is here:
http://help.yahoo.com/help/us/groups/groups-32.html

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/

<http://rd.yahoo.com/M=246920.2960106.4328965.1728375/D=egroupweb/S=1705006951:HM/A=1513704/R=0/*http://www.gotomypc.com/u/tr/yh/cpm/grp/300_02F/g22lp?Target=mm/g22lp.tmpl>

Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.html

[Non-text portions of this message have been removed]
• use POSIX isdigit ; foreach my \$value ( 999, a3a , 3aa , 3 3 ) { print isdigit( \$value ) ? \$value is a number n ... } HTH, Charles K. Clarkson -- Head
Message 4 of 11 , Apr 2 3:52 PM
use POSIX 'isdigit';

foreach my \$value ( 999, 'a3a', '3aa', '3 3' ) {
print isdigit( \$value )
? "\$value is a number\n"
: "\$value is not a number\n";
}

HTH,

Charles K. Clarkson
--
Clarkson Energy Homes, Inc.
Mobile Home Specialists
254 968-8328
• You are right! I forgot all about the is... functions. ... From: Charles K. Clarkson [mailto:cclarkson@htcomp.net] Sent: Wednesday, April 02, 2003 15:52 To:
Message 5 of 11 , Apr 2 3:59 PM
You are right! I forgot all about the is... functions.

-----Original Message-----
From: Charles K. Clarkson [mailto:cclarkson@...]
Sent: Wednesday, April 02, 2003 15:52
To: perl-beginner@yahoogroups.com
Subject: RE: [PBML] How to check if the user input is number?

use POSIX 'isdigit';

foreach my \$value ( 999, 'a3a', '3aa', '3 3' ) {
print isdigit( \$value )
? "\$value is a number\n"
: "\$value is not a number\n";
}

HTH,

Charles K. Clarkson
--
Clarkson Energy Homes, Inc.
Mobile Home Specialists
254 968-8328

<http://rd.yahoo.com/M=245454.3115308.4434529.1728375/D=egroupweb/S=1705006951:HM/A=1457554/R=0/*http://ipunda.com/clk/beibunmaisuiyuiwabei>

Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.html

[Non-text portions of this message have been removed]
• ... What you need is a loop that will continue to query the user after incorrect input. This loop runs forever. It is dangerous but can be escaped on most
Message 6 of 11 , Apr 2 4:44 PM
: The question is how could the regexp check to see if the
: value belonged to all possible values that \$i takes ???

What you need is a loop that will continue to query
the user after incorrect input. This loop runs forever.
It is dangerous but can be escaped on most systems.

my \$max_table = 25;
my \$input;
while (1) {
print "Which table (1 - \$max_table)?";
chomp( \$input = <STDIN> );
}

To break out we use the "last" function:

use POSIX 'isdigit';

my \$max_table = 25;

my \$input;
while (1) {
print "Which table (1 - \$max_table)?";
chomp( \$input = <STDIN> );

last if isdigit( \$input )
&& \$input > 0
&& \$input <= \$max_table;
}

print "\n\nTable selected: \$input times table\n";

HTH,

Charles K. Clarkson
--
Clarkson Energy Homes, Inc.
Mobile Home Specialists
254 968-8328

P.S.

A more generic approach would allow for a range
of answers and a user query. Using a subroutine we
could provide a return value. Of course, this is
probably overkill, but:

sub query_user {
my \$range = shift;
my \$query = shift;

# create a hash of valid responses
my %valid;
@valid{ @\$range } = (1) x @\$range;

while (1) {
print \$query;
chomp( my \$response = <STDIN> );
return \$response if \$valid{ lc \$response };
}
}

my \$times_table =
query_user(
[ 1 .. 25 ],
'Which table (1 - \$max_table)?'
);

my \$another table =
query_user(
[ 'yes', 'y', 'no', 'n' ],
'Another table?'
);

The disadvantage is only a finite number of
values are possible. You can't ask for any number
between 1 and 2, for example.
Your message has been successfully submitted and would be delivered to recipients shortly.