develooper Front page | perl.beginners | Postings from February 2002

RE: reading a file line by line

Thread Previous | Thread Next
Lyon, Justin
February 27, 2002 12:10
RE: reading a file line by line
Message ID:
Here's something simple:

open(FILE, "/export/home/me/filename");

while ($line = <FILE>)
   # whatever

FILE is a filehandle, and by defualt it will give you back a line at a time.
You can also put the whole thing into an array by doing this:

@file = <FILE>;

At least, I think that's it.  Somebody correct me if I'm wrong.

-----Original Message-----
From: Jon Serra []
Sent: Wednesday, February 27, 2002 11:58 AM
Subject: reading a file line by line

Greetings,  I am trying to read a text file into a scalar variable line by
until EOF or a search test if fullfilled.  I have been reading files in like


chomp (@line = `cat filename`);

I do not want to do this beacuse my file is quite large, and there is no
to hog the memory for the little bit I actually want, and second, I do not
to make a system call to the unix cat command.  TIA Jon

To unsubscribe, e-mail:
For additional commands, e-mail:

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About