use strict; use warnings; # Get file names, open the input file. # my $inputFile = q{spw592046.dat}; my $tempFile = q{spw592046.tmp}; open my $inputFH, q{<}, $inputFile or die qq{open: $inputFile: $!\n}; # Declare, open and close the temporary # file handle to clobber any file left # over from a previous run. # my $tempFH; open $tempFH, q{>}, $tempFile or die qq{open: $tempFile: $!\n}; close $tempFH or die qq{close: $tempFile: $!\n}; # Initialise count of lines written to # each temporary file. Process input file # in an infinite loop. # my $lineCt = 0; while (1) { # Read a line from input file. Is it # a header line? # my $line = <$inputFH>; if ($line =~ m{^1}) { # Process previous record. # outputData(); # Open new temporary file and print # line just read to it, counting. open $tempFH, q{>}, $tempFile or die qq{open: $tempFile: $!\n}; print $tempFH $line; $lineCt = 1; # If line just read was at end of # file then process one-line record # and exit loop. # if (eof $inputFH) { outputData(); last; } } # Not a header line but an ordinary line # at the end of file. # elsif (eof $inputFH) { # Put line in temporary file, unless # it is blank, process the record and # exit loop. # unless ($line =~ m{^\s*$}) { $lineCt ++; print $tempFH $line; } outputData(); last; } # Normal line, print to temporary file # unless blank. # else { next if $line =~ m{^\s*$}; $lineCt ++; print $tempFH $line; } } sub outputData { # If the line count is +ve we have a # temporary file to close so do so. # if ($lineCt) { close $tempFH or die qq{close: $tempFile: $!\n}; } # We've just read the very first header # so there's no record to process yet. # else { return; } # Show how many lines should be in the # temporary file. # print qq{\nExpect: $lineCt line}, $lineCt > 1 ? qq{s\n} : qq{\n}; # Open temporary file, read and print # contents and close again. # open my $cacheFH, q{<}, $tempFile or die qq{open: $tempFile: $!\n}; while (<$cacheFH>) { print qq{Cached: $_}; } close $cacheFH or die qq{close: $tempFile: $!\n}; }
the data file
100000001 200546Mary 200549#0002897 20055100001 100000003 200546Kathy 200547#0002530 200549#0002897 200552123 Elm Street 100000010 200546Joan 200547#0002530 200549#0002897 20055100001 200552681 Ocean Avenue 100000012
and the output
Expect: 4 lines Cached: 100000001 Cached: 200546Mary Cached: 200549#0002897 Cached: 20055100001 Expect: 5 lines Cached: 100000003 Cached: 200546Kathy Cached: 200547#0002530 Cached: 200549#0002897 Cached: 200552123 Elm Street Expect: 6 lines Cached: 100000010 Cached: 200546Joan Cached: 200547#0002530 Cached: 200549#0002897 Cached: 20055100001 Cached: 200552681 Ocean Avenue Expect: 1 line Cached: 100000012
I hope this is of some interest despite the late response.
Cheers,
JohnGG
In reply to Re: Ugly variable-length record handling
by johngg
in thread Ugly variable-length record handling
by Melly
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |