If you have a treatment for each line of the report you could also put them into an array of subs and then use the % operator to call them one by one:

use strict; use warnings; use Data::Dumper; my @subs; $subs[ 8 ] = sub { $_[0]->{'return1'} = substr $_[1], 0, 39 }; $subs[ 9 ] = sub { $_[0]->{'return2'} = substr $_[1], 0, 39 }; $subs[ 10 ] = sub { $_[0]->{'return3'} = substr $_[1], 0, 39 }; $subs[ 11 ] = sub { $_[0]->{'return4'} = substr $_[1], 0, 39 }; my @csv; while(<DATA>){ chomp; push @csv, {} if 1 == $. % 58; $subs[$. % 58]( $csv[-1], $_ ) if defined $subs[$. % 58]; } print Dumper \@csv; __DATA__ 06 01720168-00000000257980 123 S Somewhere HWY 192 172016-8 Company NATURAL GAS CO., INC. Business P O BOX 1547 123 Road Dr. Town ST 12345 SUITE# 1234 Town, ST 12345

Be aware, that the sub for the 58th line would be $subs[0]. If there is no action for a line, no sub needs to be specified.

This approach would only work if you really have a different approach for each line. If there are interactions between lines, then it probably cannot be fixed...

Update: fixed (removed) link to mod operator


In reply to Re: Parsing a Formatted Text File by hdb
in thread Parsing a Formatted Text File by Pharazon

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.