http://www.perlmonks.org?node_id=806443


in reply to Better way to perform "double split" ?

use strict; use warnings; use Parse::RecDescent; use Data::Dumper; my $g = Parse::RecDescent->new(<<'EOG'); main: head row(s) /\Z/ { [$item[1], @{$item[2]}]; } | <error> head: /.*/ /-*/ { $item[1]; } row: date time num { [@item[1..3]]; } date: /\d\d\d\d-\d\d-\d\d/ time: /\d\d:\d\d:\d\d/ num: /[\d\.]+/ EOG my $data = join '', <DATA>; print Dumper($g->main($data)); __DATA__ Timestamp 10.72.218.82:cpu_busy ---------------------------------------------------------------------- +--------- 2009-11-05 17:59:52 1.501 2009-10-15 17:39:52 2.501 2009-12-25 17:19:52 3.501
will return the following, skipping empty lines and spaces, or generate a detailed error if data is invalid.
$VAR1 = [ 'Timestamp 10.72.218.82:cpu_busy', [ '2009-11-05', '17:59:52', '1.501' ], [ '2009-10-15', '17:39:52', '2.501' ], [ '2009-12-25', '17:19:52', '3.501' ] ];
Update: changing the row: rule returning a DateTime will also verify if the date is correct, improving the error detection while parsing.