pattern matching question

M

mike

hi

i am asking for user input and the input must be an integer representing the
minutes of the hour or a string of minutes joined by a comma . For example
1) 15
2) 15,20,30
3) 15,30,40,50,59

The requirement to check are:
1) no words are allowed
2) number must be increasing order, ie. 40,34,2,59 is wrong
3) cannot end with comma ie. 40,41, is wrong
4) minutes in the range 0-59

printf "Enter minutes : " ;
my $minutes = <STDIN>; chomp($minutes);
next if $minutes =~ /\w+/;
next if $minutes =~ /\,$/;
if ( $minutes =~ /^[0-5][0-9]$/) {
print "ok\n";
last;
}

How do i check for multiple minutes separated by commas and must be increasin order?
thanks
 
B

Bob Walton

mike wrote:

....
i am asking for user input and the input must be an integer representing the
minutes of the hour or a string of minutes joined by a comma . For example
1) 15
2) 15,20,30
3) 15,30,40,50,59

The requirement to check are:
1) no words are allowed
2) number must be increasing order, ie. 40,34,2,59 is wrong
3) cannot end with comma ie. 40,41, is wrong
4) minutes in the range 0-59

printf "Enter minutes : " ;
my $minutes = <STDIN>; chomp($minutes);
next if $minutes =~ /\w+/;
next if $minutes =~ /\,$/;
if ( $minutes =~ /^[0-5][0-9]$/) {
print "ok\n";
last;
}

How do i check for multiple minutes separated by commas and must be increasin order?

Perhaps something like:

use warnings;
use strict;
print "Enter minutes: ";
my $input=<STDIN>;
chomp $input;
my @minutes=split ',',$input;
my $oldvalue=$minutes[0]-1;#no error on first check
for(@minutes){
die "non-numeric" unless /^\d+$/;
die "outside range of 0 to 59" unless $_>=0 and $_<=59;
die "not monotonically increasing" unless $_>$oldvalue;
$oldvalue=$_;
}
print "all is OK: ".join(',',@minutes)."\n";
 
T

Tore Aursand

i am asking for user input and the input must be an integer representing
the minutes of the hour or a string of minutes joined by a comma . For
example 1) 15
2) 15,20,30
3) 15,30,40,50,59

The requirement to check are:
1) no words are allowed
2) number must be increasing order, ie. 40,34,2,59 is wrong
3) cannot end with comma ie. 40,41, is wrong
4) minutes in the range 0-59

How do i check for multiple minutes separated by commas and must be
increasin order?

Start with the following:

my $string = <STDIN>;
chomp( $string );
unless ( $string =~ /\d+$/ ) {
die "Input must end with a number\n";
}

my @numbers = split( /,/, $string );
my $prev = 0;
foreach ( @numbers ) {
if ( /^\d+$/ ) {
if ( $_ > $prev ) {
if ( $_ >= 0 && $_ <= 59 ) {
# Seems OK
$prev = $_;
}
else {
die "$_ must be in range 0-59\n";
}
}
else {
die "$_ is not larger than $prev\n";
}
}
else {
die "$_ is not a number\n";
}
}

If you don't care about _what_ goes wrong, you could easily translate all
the if statements above to this:

foreach ( @numbers ) {
if ( /^\d+$/ && $_ > $prev && $_ >= 0 && $_ <= 59 ) {
# Seems OK
$prev = $_;
}
else {
die "There is a problem with $_\n";
}
}

Note: I didn't test this code, but it should give you a hint on how to
deal with your problem in an easy way.
 
J

Jürgen Exner

mike said:
i am asking for user input and the input must be an integer
representing the minutes of the hour or a string of minutes joined by
a comma . For example 1) 15
2) 15,20,30
3) 15,30,40,50,59

The requirement to check are:
1) no words are allowed
2) number must be increasing order, ie. 40,34,2,59 is wrong
3) cannot end with comma ie. 40,41, is wrong
4) minutes in the range 0-59

printf "Enter minutes : " ;
my $minutes = <STDIN>; chomp($minutes);
next if $minutes =~ /\w+/;
next if $minutes =~ /\,$/;
if ( $minutes =~ /^[0-5][0-9]$/) {
print "ok\n";
last;
}

How do i check for multiple minutes separated by commas and must be
increasin order?

Step 1: lexical analysis
split() your input string at the commas into individual tokens and store
them in an array. At the same time clean up any white space and other junk
Step 2: syntax analysis
for each token check if it is a valid number, i.e. a whole number between 0
and 56
Step 3: context analysis
for each number check if it is larger than the previous number

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top