how to read in line by line? 
Author Message
 how to read in line by line?

Hi,

  I am trying to write a piece of perl script to process a file.
I want to open a file and then read from it line by line.
I am trying to use the following perl script, but it seems
it only read in one line out of every two lines.  Please give
me some ideas or suggestions, Thanks!

  try.pl
< 1K Download


Fri, 02 Nov 2001 03:00:00 GMT  
 how to read in line by line?
[courtesy cc to poster]

Quote:

> Hi,

>   I am trying to write a piece of perl script to process a file.
> I want to open a file and then read from it line by line.
> I am trying to use the following perl script, but it seems
> it only read in one line out of every two lines.  Please give
> me some ideas or suggestions, Thanks!

I managed to extract some of your code out of that gorp you
attached.  I immediately noticed this:

Quote:
> while (<IN>){

>         $line = <IN>;

<> is the line-reading operator.  Every time you invoke it,
it reads one (or more, depending on context) lines.  So the
first line above reads in the first line of IN, and assigns
it to $_ .. which you throw away.  Then you read the next
line, and assign that to $line.  _ad_infinitum_.  You only
get the even records into $line like this.  Take out the
second line above, and use the $_ variable, which is
simpler.

Quote:
> --------------------------------------------------------------------------------

>                 Name: try.pl
>    try.pl       Type: Perl Program (application/x-perl)
>             Encoding: 7bit

This is the sort of stuff that shows up when you attach
stuff.  It's not easily readable, and looks ghastly for
people who have standard newsreaders.  Please, in future
remember that UseNet is a text-only medium, and do not
post MIME attachments.  Just include the [appropriately
trimmed if needed] program into your post.

Thank you,
David
--

Senior computing specialist
mathematical statistician



Fri, 02 Nov 2001 03:00:00 GMT  
 how to read in line by line?
: This is a multi-part message in MIME format.
                                  ^^^^^^^^^^^

   Please configure your "newsreader" correctly.

   Usenet is a text-only medium. MIME doesn't belong here.

:   I am trying to write a piece of perl script to process a file.
: I want to open a file and then read from it line by line.

   The first example given in the description for open() in
   perlfunc.pod shows how to do that.

: I am trying to use the following perl script, but it seems
: it only read in one line out of every two lines.

   No, it reads every line.

   Your code discards every other line though.

:   while (<IN>){

   Read a line. Put it into the $_ special variable, never to be used...

:         $line = <IN>;

   Read *another* line. Process this one...

--
    Tad McClellan                          SGML Consulting

    Fort Worth, Texas



Fri, 02 Nov 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

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

2. how to read in line by line?

3. read line by line

4. Limiting maximum line length during line based read

5. read line by line in perl

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

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

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

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

10. Line by line file reading

11. How to read a file line by line w/loop

12. Read remote text file line by line

 

 
Powered by phpBB® Forum Software