Let's see if I can do something while I read your specification.

# "What I essentially need is a way to say..." my $start_tag = 'BEGIN'; my $end_constant = 'END'; my $headerformat = join('; ', "%s=%s" x 17)."\n"; # XXX ? format for +printf() ? my $headerfile = 'header000'; my $detailsection = 'detail000'; open IN, '<', $file) or die "Can't read '$file': $!\n"; RECORD: while(defined($_ = <IN>)) { my %out; # we'll capture the "record content" for pr +intf() here if (/$start_tag/) { # "...start at this character..." $. = 0; # reset line counter while(<IN>) { # "...read each line for the next 17..." # XXX the specs aren't clear he +r. my ($key, $value) = split; # so I'll just split $out{$key} = $value; # "...assign to values to do # a printf statement..." if($. == 17) { # done with reading the record. do "the pri +ntf()" open HEADER, '>', $headerfile or die "Can't write to '$headerfile': $!\n"; printf HEADER $headerformat, map { $_,$out{$_} } keys +%out; close HEADER; $headerfile++; # string increment: header000 -> he +ader001 $. = 0; open DETAIL, '>', $detailsection or die "Can't write to '$detailsection': $!\n"; # "starting at the 18th line of tha +t record..." while(<IN>) { # "...read until the end constant.. +." print DETAIL; # "...and print that straight to # a different file..." last if /$end_constant/; } close DETAIL; $detailsection++; next RECORD; # "...and then start the entire l +oop again." } } } }

Poo. That might work, but it's butt ugly and hard to read. Let's refactor that a bit.

open my $fh, '<', $file) or die "Can't read '$file': $!\n"; while(<$fh>) { write_record($fh,$headerfile) if /$start_tag/; $headerfile++; } sub write_record { my ($fh, $outfile) = @_; my %out; $. = 0; while(<$fh>) { my ($key, $value) = split; $out{$key} = $value; if ($. == 17) { open my $header, '>', $outfile or die "Can't write '$outfile': $!\n"; printf $header $headerformat, map { $_,$out{$_} } keys %ou +t; close $header; write_detail($fh,$detailsection); $detailsection++; return; } } } sub write_detail { my ($fh, $detailfile) = @_; open my $detail, '>', $detailfile or die "Can't write to '$detailfile': $!\n"; while(<$fh>) { last if /$end_constant/; print $detail; } close $detail; }

Does that make sense to you? If it doesn't, write better specifications ;-)

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

In reply to Re: File read and re-ordering by shmem
in thread File read and re-ordering by KarmicGrief

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.