in reply to Re^2: handling erronous input
in thread handling erronous input
You've missed part of the regexp out (the bit that captures comments) and missed a backslash out (from \d{5}). It looks like you don't know that, in a regexp, parentheses capture their matches into $1, $2, $3 etc. Again, see perlretut and perlre for the details.
Your code is similar to mine. You use
while ( ... ) { unless ( some-condition ) { next } some-code }
while I prefer the equivalent
while ( ... ) { if ( some-condition ) { some-code } }
it's just that (IMHO) yours is harder to read (and longer, too)
That said, you can use my code with a filehandle like so (I've rearranged it a bit to use unless and made the regexp a more lenient towards spaces)...
while ( <DATAFILE> ) { chomp; unless ( m{^ (\d\.\d{5}) \s*,\s* (\d\d:\d\d:\d\d) \s* (.*) $ }x ) +{ next } my ( $quote, $time, $comment ) = ( $1, $2, $3 ); # captures my ( $hours, $minutes, $seconds ) = split /:/, $time; #do something with $quote, $hours, $minutes, $seconds & $comment }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: handling erronous input
by GrandFather (Saint) on Apr 06, 2008 at 23:35 UTC | |
|
Re^4: handling erronous input
by Conal (Beadle) on Apr 06, 2008 at 23:50 UTC |