Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

My approach when parsing lists depends on the nature of the list.

If I can find out from looking at a single line what kind it is, then I use regular expressions to fill out a hash and flush the record whenever a new set starts.

If I can't find out from looking at a single line what kind it is, I use counters or flags to know what line I am on.

In your case, it looks to me as if you basically have a report with some header data and then three payload lines, the issuance date, the address and the description. One ugly thing seems to be that the address and the permission can span multiple lines, but from the unrepresentative example you have posted, each item seems to be delimited by a blank line from the previous item.

Going from these assumptions, my approach would be something like the following (untested):

#!perl -w use strict; use Data::Dumper; # Output a row of information sub flush { my( $record ) = @_; if( $record->{permit} ) { print Dumper $record; }; delete $record->{permit}; }; # This will collect all information for one entry: my %info; my $last_page; my $expected_pages; my $record_kind; my %next_record = ( address => 'description', description => undef, ); while(<DATA>) { if( m!^Page (\d+) of (\d+)! ) { $last_page = $1; $expected_pages ||= $2; next; }; if( m!^(Jan|Feb|...|Jun|...) (19\d\d|20\d\d)! ) { $info{ report_date } = "$2-$1"; next; }; # ... more code to skip the header left for the reader next if( m!MONTHLY EXTERNAL MODIFICATIONS PERMITS REPORT! ); next if( m!^Permit Issued! ); if( m!^(\d\d)/(\d\d)/((?:19|20)\d\d)$! ) { flush(\%info); $info{ permit } = "$3-$2-$1"; $record_kind = 'address'; <>; # skip empty line next }; # Fast-forward until the next set of lines if( $record_kind ) { while( <> !~ /^\s*$/ ) { s!\s*$!!; $info{ $record_kind } .= " " . $_; }; $record_kind = $next_record{ $record_kind }; } else { die "Unknown line [$_] on line $."; }; }; warn "Uhoh - expected $expected_pages but only read up to $last_page" if( $expected_pages != $last_page ); __DATA__ Page 1 of 3 100 Civic Center Way Calabasas, California 91302 7/12/2012 9:21:02AM MONTHLY EXTERNAL MODIFICATIONS PERMITS REPORT Jun 2012 Permit Issued Address Description 06/01/2012 26166 ROYMOR DR Upgrade panel from 100 amp to 200 amp 06/04/2012 24956 NORMANS WAY (6) light fixtures @ patio; (3) branch circuits; (4) electric heaters 06/05/2012 4273 VICASA DR Construct 339 SF Covered Loggia

In reply to Re: creating array of hashes from input file by Corion
in thread creating array of hashes from input file by chimiXchanga

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2024-04-25 19:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found