Melly has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monkees
I have a large input file. Every time a line starts with a "1", I want to handle the preceding lines up to the preceding "1" (a "1" indicates the start of a different subject's data).
Because of the file-sizes involved, as well as other unavoidable memory requirements, I'm writing out the subject's data to a temporary file (rather than trying to hold all the lines in memory).
All okay - except my handling of new records is, IMHO, clumsy, and leads to an extra call to the subroutine, "output_data" at the end of the process (to handle the final record).
Here is the simplified code:
$counter = 1; open(DATA, "export.dat")||die "Cannot open export.dat for read:$!\n"; while(<DATA>){ $temp_line = $_; if($temp_line =~ /^1(\d*)/){ if($counter > 1 ){ #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"; print TEMP $temp_line; $counter ++; } elsif(/\S+/){ print TEMP $temp_line; } } close DATA||die "Cannot close $in_dir/export.dat (weird):$!\n"; close TEMP||die "Cannot close $in_dir/temp.dat:$!\n"; &output_data(); sub output_data{ #do stuff with temp.dat }
Can anyone suggest a more elegant way to handle this kind of thing?
Oh, and BTW, I've just noticed that I call my filehandle "DATA" - does this create any potential problems? Hmm, could be confusing to whoever maintains the code if they know about __DATA__ - better change it I guess....
Tom Melly, pm@tomandlu.co.ukmap{$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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Ugly variable-length record handling
by roboticus (Chancellor) on Dec 28, 2006 at 14:28 UTC | |
by Melly (Chaplain) on Dec 28, 2006 at 15:04 UTC | |
|
Re: Ugly variable-length record handling
by jettero (Monsignor) on Dec 28, 2006 at 14:08 UTC | |
by Melly (Chaplain) on Dec 28, 2006 at 14:22 UTC | |
by jettero (Monsignor) on Dec 28, 2006 at 14:33 UTC | |
by Melly (Chaplain) on Dec 28, 2006 at 15:02 UTC | |
|
Re: Ugly variable-length record handling
by alpha (Scribe) on Dec 28, 2006 at 15:07 UTC | |
by Melly (Chaplain) on Dec 28, 2006 at 15:41 UTC | |
|
Re: Ugly variable-length record handling
by Limbic~Region (Chancellor) on Dec 28, 2006 at 18:20 UTC | |
|
Re: Ugly variable-length record handling
by johngg (Canon) on Dec 30, 2006 at 23:50 UTC | |
|
Re: Ugly variable-length record handling
by graff (Chancellor) on Jan 01, 2007 at 21:28 UTC | |
by Melly (Chaplain) on Jan 04, 2007 at 09:58 UTC |