I was going through Sam's Teach Yourself Perl in 24 hours and I got to the end of hour 6, the regex section, and I see this activity:
See whether you can produce a pattern to match a standard time format.
+ All
the following should be acceptable: 12:00am, 5:00pm, 8:30AM. These sho
+uld probably not be accepted: 3:00,2:60am, 99:00am, 3:0pm.
OK, easy enough:
#!/usr/bin/perl -w
use strict;
use warnings;
print "Type in the current time!\n";
my $input = <STDIN>;
while($input)
{
if($input =~ m/(\d)*(\d):(\d)(\d)(\w)(\w)/ | $input =~ m/(\d)*(\d)
+:(\d)(\d)(\s)(\w)(\w)/)
{
if($1 > 1 || $2 > 2 || $3 > 6)
{
print "That is not a valid time!\n";
print "Type the time again.\n";
$input = <STDIN>;
}
else
{
print "That is a valid time!\n";
$input = 0;
}
}
else
{
print "Please be more specific.\n";
$input = <STDIN>;
}
}
However, this is the terminal output:
Last login: Thu Aug 18 11:28:44 on ttys000
youngs-mac-mini:~ fenimore$ cd ~/Documents
youngs-mac-mini:Documents fenimore$ perl TimeChecker.pl
Type in the current time!
4:90
Please be more specific.
4:30AM
Use of uninitialized value $1 in numeric gt (>) at TimeChecker.pl line
+ 13, <STDIN> line 2.
That is not a valid time!
Type the time again.
12:30PM
That is a valid time!
youngs-mac-mini:Documents fenimore$
Er... any help here?
For added clarity, this is line 13:
if($1 > 1 || $2 > 2 || $3 > 6)