in reply to A Slough of ParseRecDescent Woes
All that being said, I'm not sure I'd actually use Parse::RecDescent for this problem. I might just quickly get the first field, and use that as a key to a hash of subroutines which use regexes to parse the data and return the results. I'd consider how much you care about efficiency in this routine anyway.use Parse::RecDescent; use strict; use warnings; use Data::Dumper; # Make sure the parser dies when it encounters an error $::RD_ERRORS = 1; # Enable warnings. This will warn on unused rules &c. $::RD_WARN = 1; # Give out hints to help fix problems. $::RD_HINT = 1; # Create and compile the source file my $parser = Parse::RecDescent->new( q( comma : "," date : /\b\d{1,2}\/\d{1,2}\/\d{1,2}\b/ start_date : date end_date : date time : /\b\d\d:\d\d:\d\d\b/ rate : /\b\d+\.\d{4}\b/ rates : rate comma { $item{rate} } start_rate : rate end_rate : rate change : rate whitespace : /\s*/ lines : line /\z/ { $item{line} } line : "G017RATEBRKRL" comma rate comma start_date comma end_date comma time { $item{type} = $item[0]; \%item } line : "G017CP111 D" comma start_rate comma end_rate comma change comma date comma time { \%item } line : "G017RPAGO/N" comma rate comma whitespace comma whitespace comma date comma time { \%item } line : "G017ONFD" comma rates(6) date comma time { \%item } line : "G017PDFF" comma rates(4) date comma time { \%item } ) ); while ( my $quote_data = <DATA> ) { next if $quote_data !~ /\S/; my $result = $parser->lines( $quote_data ); if ( defined $result ) { print Dumper $result; } else { print "Failed!\n"; } }
|
|---|