Beefy Boxes and Bandwidth Generously Provided by pair Networks chromatic writing perl on a camel
more useful options
 
PerlMonks  

Re: Regular expression and lexical variables

by wfsp (Abbot)
on Apr 17, 2011 at 07:22 UTC ( #899799=note: print w/ replies, xml ) Need Help??


in reply to Regular expression and lexical variables

You have ; as well as : in your data. Either fix your data or modify the regex.

#!/usr/bin/perl use v5.12.2; use warnings; use strict; while (<DATA>) { $_ =~ /(\d\d)[:;](\d\d)[:;](\d\d)/; my $hours = $1; my $minutes = $2; my $seconds = $3; print "$hours,$minutes,$seconds\n" } __DATA__ 01:23:45 06:54;32 23;45:16
update: You could introduce some error checking
#!/usr/bin/perl use v5.12.2; use warnings; use strict; while (my $line = <DATA>) { if ($line =~ /(\d\d)[:;](\d\d)[:;](\d\d)/){ my $hours = $1; my $minutes = $2; my $seconds = $3; print "$hours,$minutes,$seconds\n"; } else{ print qq{bad line: $line\n}; } } __DATA__ 01:23:45 06:54;32 23;45:16 23_45_16


Comment on Re: Regular expression and lexical variables
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://899799]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (18)
As of 2014-04-16 17:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (433 votes), past polls