For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2006 > reading a file









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author reading a file
Gerald Host

2006-10-19, 6:56 pm

I'm trying to read a text file line-by-line.

open IN, shift;
my @lines=split("\n",<IN> );
foreach my $line (@lines) {
print OUT "QQQ $line QQQ\n";
}

The problem is that it always gives me the entire file, not line by line.
What did I do wrong? Other things I've tried:

open IN, shift;
while (my $line =<IN> ) {
print OUT "QQQ $line QQQ\n";
}


Ryan

DJ Stunks

2006-10-19, 6:56 pm

Gerald Host wrote:
> I'm trying to read a text file line-by-line.
>
> open IN, shift;
> my @lines=split("\n",<IN> );
> foreach my $line (@lines) {
> print OUT "QQQ $line QQQ\n";
> }
>
> The problem is that it always gives me the entire file, not line by line.
> What did I do wrong? Other things I've tried:
>
> open IN, shift;
> while (my $line =<IN> ) {
> print OUT "QQQ $line QQQ\n";
> }
>


and how did that second try work out for you?

-jp

Gerald Host

2006-10-19, 6:56 pm

I tried both, and they typically do work for me, but right now they just
aren't...

QQQ line1....
line2
line3
....
lineX QQQ

any ideas?

Ryan

On 10/19/06, Helliwell, Kim <Kim.Helliwell@lsi.com> wrote:
>
> Another way:
>
> foreach $line (<IN> )
> {
> ...
> }
>
> if you don't want to slurp all the lines into an array (to save memory).
>
> Kim Helliwell
> LSI Logic Corporation
> Work: 408 433 8475
> Cell: 408 832 5365
> kim.helliwell@lsi.com
>
> Please Note: My email address will change to kim.helliwell@lsi.com on
> Oct 14. The old 'lsil.com' email address will stop working after Jan 15,
> 2007. Please update your address book and distribution lists
> accordingly. Thank you.
>
> -----Original Message-----
> From: Gerald Host [mailto:ghost@madisonip.com]
> Sent: Thursday, October 19, 2006 3:35 PM
> To: Perl List
> Subject: reading a file
>
> I'm trying to read a text file line-by-line.
>
> open IN, shift;
> my @lines=split("\n",<IN> );
> foreach my $line (@lines) {
> print OUT "QQQ $line QQQ\n";
> }
>
> The problem is that it always gives me the entire file, not line by
> line.
> What did I do wrong? Other things I've tried:
>
> open IN, shift;
> while (my $line =<IN> ) {
> print OUT "QQQ $line QQQ\n";
> }
>
>
> Ryan
>


Jenda Krynicky

2006-10-19, 6:56 pm

From: "Gerald Host" <ghost@madisonip.com>
> I'm trying to read a text file line-by-line.
>
> open IN, shift;
> my @lines=split("\n",<IN> );


Did you ever read the docs???

my @lines=<IN>;

The <> operator returns the list of lines in the file if called in
the list context.

> foreach my $line (@lines) {
> print OUT "QQQ $line QQQ\n";
> }
>
> The problem is that it always gives me the entire file, not line by
> line. What did I do wrong? Other things I've tried:
>
> open IN, shift;
> while (my $line =<IN> ) {
> print OUT "QQQ $line QQQ\n";
> }


This one should work.

Are you sure you did not touch $/ beforehand? This is the variable
that specifies what's the line delimiter. If you undefine it you'll
get the whole file as the first and only line.

Jenda
===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

usenet@DavidFilmer.com

2006-10-19, 6:56 pm

Gerald Host wrote:
> open IN, shift;


Always check and report failure on I/O operations:

open IN, shift or die "Oops - $!\n";

(and use lexical variables for filehandles).

--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)

Xavier Mas

2006-10-19, 9:56 pm

A Divendres 20 Octubre 2006 00:34, Gerald Host va escriure:
> I'm trying to read a text file line-by-line.
>
> open IN, shift;
> my @lines=split("\n",<IN> );
> foreach my $line (@lines) {
> print OUT "QQQ $line QQQ\n";
> }
>
> The problem is that it always gives me the entire file, not line by line.
> What did I do wrong? Other things I've tried:
>
> open IN, shift;
> while (my $line =<IN> ) {
> print OUT "QQQ $line QQQ\n";
> }
>
>
> Ryan


Im my @lines you are giving all the file in one shot. read de the file line by
line using a while loop inserting all last chunk of code into it.

--
Xavier Mas
Kim Helliwell

2006-10-20, 3:58 am

Try using:

my @lines =3D <IN>;

I don't think you need the split, and it's goofing things up.
I know the above works, because I use it all the time.

Kim Helliwell
LSI Logic Corporation
Work: 408 433 8475
Cell: 408 832 5365
kim.helliwell@lsi.com
=20
Please Note: My email address will change to kim.helliwell@lsi.com on
Oct 14. The old 'lsil.com' email address will stop working after Jan 15,
2007. Please update your address book and distribution lists
accordingly. Thank you.

-----Original Message-----
From: Gerald Host [mailto:ghost@madisonip.com]=20
Sent: Thursday, October 19, 2006 3:35 PM
To: Perl List
Subject: reading a file

I'm trying to read a text file line-by-line.

open IN, shift;
my @lines=3Dsplit("\n",<IN> );
foreach my $line (@lines) {
print OUT "QQQ $line QQQ\n";
}

The problem is that it always gives me the entire file, not line by
line.
What did I do wrong? Other things I've tried:

open IN, shift;
while (my $line =3D<IN> ) {
print OUT "QQQ $line QQQ\n";
}


Ryan
Kim Helliwell

2006-10-20, 3:58 am

Another way:

foreach $line (<IN> )
{
....
}

if you don't want to slurp all the lines into an array (to save memory).

Kim Helliwell
LSI Logic Corporation
Work: 408 433 8475
Cell: 408 832 5365
kim.helliwell@lsi.com
=20
Please Note: My email address will change to kim.helliwell@lsi.com on
Oct 14. The old 'lsil.com' email address will stop working after Jan 15,
2007. Please update your address book and distribution lists
accordingly. Thank you.

-----Original Message-----
From: Gerald Host [mailto:ghost@madisonip.com]=20
Sent: Thursday, October 19, 2006 3:35 PM
To: Perl List
Subject: reading a file

I'm trying to read a text file line-by-line.

open IN, shift;
my @lines=3Dsplit("\n",<IN> );
foreach my $line (@lines) {
print OUT "QQQ $line QQQ\n";
}

The problem is that it always gives me the entire file, not line by
line.
What did I do wrong? Other things I've tried:

open IN, shift;
while (my $line =3D<IN> ) {
print OUT "QQQ $line QQQ\n";
}


Ryan
Anshul Saxena

2006-10-20, 6:56 pm

Hi add this line before your code
$/ = "\n";
What this does is breaks your file reading sequence at every new line so
that each new line is stored as a separate item in the array you are using.

Lemme know if this helps or doesn't.

On 10/19/06, Gerald Host <ghost@madisonip.com> wrote:
>
> I tried both, and they typically do work for me, but right now they just
> aren't...
>
> QQQ line1....
> line2
> line3
> ...
> lineX QQQ
>
> any ideas?
>
> Ryan
>
> On 10/19/06, Helliwell, Kim <Kim.Helliwell@lsi.com> wrote:
>
>


Paul Lalli

2006-10-20, 6:56 pm

Kim Helliwell wrote:
> Another way:
>
> foreach $line (<IN> )
> {
> ...
> }
>
> if you don't want to slurp all the lines into an array (to save memory).


You are extraordinarily . This DOES slurp the entire file into
memory. It builds an in-memory list of all the lines of the file, and
only *then* does it proceed to process each element of this list.

The way to avoid slurping the file is to use while rather than foreach:

while (my $line = <IN> ) {
#...
}

Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com