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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.