McMahon has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks...

I have some code that does List::Compare on the contents of two files. I realized, though, that there's no reason why I couldn't create the second array in memory and save myself having to write a second file to disk. However, these statements don't seem to produce the same array:
open COMP, "+>$file"; print COMP "$var1\n"; push @comp, "$var1\n"; <some stuff> print COMP "\t$var2\n"; push @comp, "\t$var2\n"; my @array = <COMP>
when it's all finished,
@array ne @comp
...and that just doesn't seem right. I'd like to understand what's going on here. I hope I've provided enough code.

Replies are listed 'Best First'.
Re: print vs push?
by Roy Johnson (Monsignor) on Aug 20, 2004 at 15:46 UTC
    Before you read from the file you've been writing to, you need to seek COMP, 0, 0

    Caution: Contents may have been coded under pressure.
Re: print vs push?
by Fletch (Bishop) on Aug 20, 2004 at 15:46 UTC

    Unless you seek() back to the beginning @array will be empty since COMP will be sitting at the end of the file.

Re: print vs push?
by lhoward (Vicar) on Aug 20, 2004 at 15:47 UTC
    You need to reset the file pointer to BOF before reading in COMP. You can do that with seek (if you opened the file in read/write mode to begin with), or closed and re-opened the file in read mode. L