Melly:

Actually, that doesn't look too bad. I'd change a few minor things to make it look roughly like this: (UNTESTED!)

#!/usr/bin/perl -w use strict; use warnings; my $counter=0; open(DATA, "export.dat")||die "Cannot open export.dat for read:$!\n"; while(<DATA>){ if (/^1(\d*)/) { if ($counter > 0) { #No temp file yet if this is the first record close TEMP||die "Cannot close temp.dat:$!\n"; &output_data(); } open(TEMP, ">$temp.dat")||die "Can't open temp.dat:$!\n"; $counter = 0; } if (/\S+/) { print TEMP $temp_line; ++$counter; } } close DATA||die "Cannot close $in_dir/export.dat (weird):$!\n"; if ($counter > 0) { close TEMP||die "Cannot close $in_dir/temp.dat:$!\n"; } &output_data(); sub output_data{ #do stuff with temp.dat }
The main change is that I consolidated the write to the temp file to a single place to clarify things. That way, the special case handler is smaller and easier to read, and all the writes appear in the same location. (Handy, if you need to change it, so it's changed in a single location.)

--roboticus


In reply to Re: Ugly variable-length record handling by roboticus
in thread Ugly variable-length record handling by Melly

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.