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

RE: reading a file line by line

Thread Previous | Thread Next
From:
Lyon, Justin
Date:
February 27, 2002 12:10
Subject:
RE: reading a file line by line
Message ID:
A1CA89C7CC40D51195FD00805FA77B5B015F05B2@casfodc08s275.bankamerica.com
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 [mailto:jonserra@hotmail.com]
Sent: Wednesday, February 27, 2002 11:58 AM
To: beginners@perl.org
Subject: reading a file line by line


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

this:

chomp (@line = `cat filename`);

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

-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About