Actually, that doesn't look too bad. I'd change a few minor things to make it look roughly like this: (UNTESTED!)
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.)#!/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 }
In reply to Re: Ugly variable-length record handling
by roboticus
in thread Ugly variable-length record handling
by Melly
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |