How to read a file line by line w/loop 
Author Message
 How to read a file line by line w/loop

I am trying to read lines from a delimited text file.  Basically, I have no
problem extracting the first line from the file, formatting it, and printing it
out.  HOwever, I need to be able to go to the next line and do the same thing.  I've checked the man pages, and they don't seem to help me.

I have the lines delimited with a % symbol, and the elements in the lines
delimited by a ^ symbol.  I'm using the split function to assign variable names
to each element, so that's not a problem.  What I need to to is create a loop
to ignore the previous line and check the next line.  I also need to look at the last line and see if it matches.  How can I do this?  ANy ideas?

Thanks.

John Coleman



Tue, 16 Mar 1999 03:00:00 GMT  
 How to read a file line by line w/loop

Quote:

> I am trying to read lines from a delimited text file.  Basically, I have no
> problem extracting the first line from the file, formatting it, and printing it
> out.  HOwever, I need to be able to go to the next line and do the same thing.

Uh, do you mean something like:

  open F, "file.txt" or die "couldn't open: $!";
  while (defined($line = <F>)) {
    print "the words on line $. are: ", join ",", split /\s+/, $line;
    print "\n";
  }

I don't want to sound harsh, but I don't think you're reading the book
or the man pages very closely, or else you're starting midway toward
the back.  ;-)

--
76% of all CGI questions posted in comp.lang.perl.misc are answered by:
"CGI.pm.  LWP.  http://www.perl.com/CPAN/modules/01modules.index.html."
....

Proprietor, 5 Sigma Productions          P.O. Box 6250 Chandler AZ 85246
Perl instruction (http://www.5sigma.com/perl/), C++/C/Perl software,



Tue, 16 Mar 1999 03:00:00 GMT  
 How to read a file line by line w/loop

Quote:
> Uh, do you mean something like:

>   open F, "file.txt" or die "couldn't open: $!";
>   while (defined($line = <F>)) {
>     print "the words on line $. are: ", join ",", split /\s+/, $line;
>     print "\n";
>   }

You can go even simpler:
---begin code block---
$/ = "%";
open(FILE,"file.txt") || die "Horribly\n";
while (<FILE>) {
        $elements[$.] = [ split(/\^/) ]; # provided you have perl 5
Quote:
}

print $elements[2][2], "\n";
----end code block----
This also answers you question better.

element number -1.


Tue, 16 Mar 1999 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Reading lines of a file within a loop?

2. How do I read line by line of data from a file using perl

3. how to read a huge file line by line without loading it into memory

4. reading lines from one file and pulling matching lines in another

5. newbie perl question - reading a file line by line

6. Line by line file reading

7. Read remote text file line by line

8. reading a file line by line backwards

9. read last line without reading previous lines, how?

10. Reading another line in an input loop?

11. how to read in line by line?

12. read line by line

 

 
Powered by phpBB® Forum Software