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

Re: reading a file line by line

Thread Previous | Thread Next
Hernan Freschi
March 4, 2002 23:30
Re: reading a file line by line
Message ID:
There's a simpler way, if you  specify the file from the command line (as in file) you can do

while (<>) {
    last if /search test/;
    #do something

in fact, you can make a cat-like script like this:

while (<>) {
<> is the diamond operator, and it's the filehandle for standard input (be
it keyboard, pipe or file).

Hernan Freschi

"Jon Serra" <> escribió en el mensaje
> 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
> this:
> 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

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