in reply to Ugly variable-length record handling

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

Replies are listed 'Best First'.
Re^2: Ugly variable-length record handling
by Melly (Chaplain) on Dec 28, 2006 at 15:04 UTC

    Thanks roboticus - the change you suggest is minor but welcome. The way I handle the new records still bugs me, but it looks like that may be as good as it gets...

    map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;map{($d,$e)=(2*$d*$e+$a,$e**2 -$d**2+$b);$c=$d**2+$e**2>4?$d=8:_}1..50;print$c}0..59;print$/}0..20
    Tom Melly, pm@tomandlu.co.uk