[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Re: "rewinding" a file



John Springer writes:
> 
> So what I'm doing is finding records in the main file and writing them out
> to a temp file with $keyfield in front (I compute the $keyfield based on
> some other stuff).
> Simultaneously (approximately) I push the $keyfield onto a @list.
> Then I sort the @list.
> Then I open the temp file and scan through it looking for matches to the
> first record in the @list.
> Then I repeat for the next entry in @list.
> etc.
> 

Hi!

A better way involve the perl functions tell & seek, consult the man-pages.
The idea is to keep the position in the tmp file for every $keyfield.
IMHO, an associative array do all the job.

Pseudo-code:

for each input do {
        $keyArray{$keyfield} = tell TMP;
        print TMP "all the stuff for $keyfield";
}

foreach (sort keys %keyArray) {
        seek TMP $keyArray{$_};
        $in = <TMP>;
        print OUT $in;
        }

:edoc-oduesP

... or some such.
As always, "the details are left as an axercise to the reader", and we all
know the amount of work hidden by such phrase ;-)

Greetings,
        dna