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)

In reply to Weird Regexes Stuff by slinky773

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.