Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

print vs push?

by McMahon (Chaplain)
on Aug 20, 2004 at 15:39 UTC ( [id://384631]=perlquestion: print w/replies, xml ) Need Help??

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://384631]
Approved by roju
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-03-28 17:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found