Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Readline using foreach and while

Reply
Thread Tools

Readline using foreach and while

 
 
Saurabh Jain
Guest
Posts: n/a
 
      03-25-2008
Hi,
Is there any difference in reading a file using a while or a
foreach in perl?

If I do :
foreach(<filehandle>) {
my $local = <filehandle>; # I assumed I will increment the file
descriptor here
print " local $local\n";
}

But if I do :
while(<filehandle>) {
my $local = <filehandle>; # I assumed I will increment the file
descriptor here
print " local $local\n";
}
It works fine....
Is there something wrong or some difference in the two operations? Or
am I missing something?

Thanks and Regards,
Saurabh


Small example to replicate the issue
my file name is test.pl

#!/usr/bin/perl

open (handle,"test.pl")||die "\n $0 Could not open $! \n";

my $line = <handle>;#read a line till \n or eof
print " line $line";
#foreach(<handle>){ # Not as expected
while(<handle>){ # Works as expected
$line =<handle>;#read a line till \n or eof

print " in side $line";
$line =<handle>;#read a line till \n or eof
print " in side $line";
$line =<handle>;#read a line till \n or eof
}
close handle;
 
Reply With Quote
 
 
 
 
Ben Bullock
Guest
Posts: n/a
 
      03-25-2008
On Mar 25, 4:25 pm, Saurabh Jain <hundredr...@gmail.com> wrote:
> Hi,
> Is there any difference in reading a file using a while or a
> foreach in perl?


The foreach version seems to first read the whole of the file into an
array, and then go through it line by line:

#!/usr/bin/perl
#use warnings;
use strict;
open (handle,"testangleop.pl") or die "$0 Could not open $!";

my $line = <handle>; #read a line till \n or eof
print "0 line $line";
foreach (<handle>) { # Not as expected
#while(<handle>){ # Works as expected
print $_;
$line =<handle>; #read a line till \n or eof
print "1 in side $line";
$line =<handle>; #read a line till \n or eof
print "2 in side $line";
$line =<handle>; #read a line till \n or eof
print "3 in side $line";
}

The while seems to increment through the loop.

See also

http://www.unix.org.ua/orelly/perl/prog3/ch02_11.htm
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      03-25-2008
Saurabh Jain wrote:
> Is there any difference in reading a file using a while or a
> foreach in perl?


Yes.

foreach (<FILEHANDLE>)

reads the whole file at once, and creates a list in memory of all the
lines, so that method is inefficient and not recommended in most cases.

while (<FILEHANDLE>)

reads one line at a time.

Please study "perldoc perlsyn" for more comprehensive descriptions.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      03-25-2008
Ben Bullock wrote:
> On Mar 25, 4:25 pm, Saurabh Jain <hundredr...@gmail.com> wrote:
>> Hi,
>> Is there any difference in reading a file using a while or a
>> foreach in perl?

>
> The foreach version seems to first read the whole of the file into an
> array, and then go through it line by line:


perldoc -q "What is the difference between a list and an array"


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      03-25-2008
Saurabh Jain <> wrote:
> Is there any difference in reading a file using a while or a
>foreach in perl?


Yes. Reading a file line by line works only with while. foreach reads the
whole file at once.

>If I do :
> foreach(<filehandle>) {
> my $local = <filehandle>; # I assumed I will increment the file


You just tried to read from a file that is at EOF already.

>descriptor here
> print " local $local\n";
>}
>
>But if I do :
>while(<filehandle>) {
> my $local = <filehandle>; # I assumed I will increment the file


You are alternating between reading one line into $_ (in the while
condition) and one line into $local. Is this what you meant to do?

>descriptor here
> print " local $local\n";
>}


>
>Small example to replicate the issue
>my file name is test.pl
>
>#!/usr/bin/perl
>
>open (handle,"test.pl")||die "\n $0 Could not open $! \n";
>
>my $line = <handle>;#read a line till \n or eof
>print " line $line";
>#foreach(<handle>){ # Not as expected
>while(<handle>){ # Works as expected
>$line =<handle>;#read a line till \n or eof
>
>print " in side $line";
>$line =<handle>;#read a line till \n or eof
>print " in side $line";
>$line =<handle>;#read a line till \n or eof


And here you are reading one line into $_ (in the while condition) and then
successively three lines into $line. This may make sense if you know that a
data set has a fixed format of always 4 lines. But in 99% of all cases it's
a bug.
As for the foreach version: it already slurps the whole file into a list,
therefore there is nothing left that could be read into any of the $line.

jue
 
Reply With Quote
 
Ben Bullock
Guest
Posts: n/a
 
      03-25-2008
On Tue, 25 Mar 2008 11:59:23 +0000, John W. Krahn wrote:

> Ben Bullock wrote:


>> The foreach version seems to first read the whole of the file into an
>> array, and then go through it line by line:

>
> perldoc -q "What is the difference between a list and an array"


Found in /usr/local/lib/perl5/5.10.0/pod/perlfaq4.pod
What is the difference between a list and an array?

An array has a changeable length. A list does not.

If I had written "the foreach version reads the whole of the file into a
list", I would have contradicted this FAQ entry, which says I can't read
things into a list, because reading things into a list would change the
list's length, and "a list does not" have a changeable length.

 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      03-25-2008

Quoth Ben Bullock <>:
> On Tue, 25 Mar 2008 11:59:23 +0000, John W. Krahn wrote:
> > Ben Bullock wrote:

>
> >> The foreach version seems to first read the whole of the file into an
> >> array, and then go through it line by line:

> >
> > perldoc -q "What is the difference between a list and an array"

>
> Found in /usr/local/lib/perl5/5.10.0/pod/perlfaq4.pod
> What is the difference between a list and an array?
>
> An array has a changeable length. A list does not.
>
> If I had written "the foreach version reads the whole of the file into a
> list", I would have contradicted this FAQ entry, which says I can't read
> things into a list, because reading things into a list would change the
> list's length, and "a list does not" have a changeable length.


No, you're misunderstanding. A list is immutable *after it has been
created*. Obviously you can create lists with any contents, otherwise
you would be limited to using only lists compiled into perl. foreach
accepts a list as argument and iterates over it; <> in list context
(which is the real problem here) reads the entire file, splits it on
newline (or rather $/), and returns a (newly created) list with the
results. You can't modify the list after that: for an example where you
can, see Tie::File, which reads a file into an *array* instead.

Ben

 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      03-25-2008

Quoth Frank Seitz <>:
> Ben Morrow wrote:
> > Quoth Ben Bullock <>:
> >>On Tue, 25 Mar 2008 11:59:23 +0000, John W. Krahn wrote:
> >>>Ben Bullock wrote:
> >>>>
> >>>>The foreach version seems to first read the whole of the file into an
> >>>>array, and then go through it line by line:
> >>>
> >>>perldoc -q "What is the difference between a list and an array"

>
> Hm. Why is this distinction relevant here?


I'm not at all sure it is, except in the 'variable vs. value' sense.

> > A list is immutable *after it has been
> > created*. Obviously you can create lists with any contents, otherwise
> > you would be limited to using only lists compiled into perl. foreach
> > accepts a list as argument and iterates over it;

>
> use strict;
> use warnings;
>
> my @a = qw/a b c/;
> for my $v (@a) {
> push @a,'d' if $v eq 'c';
> print "$v\n";
> }


Good point. for is a little weird in this respect...

Ben

 
Reply With Quote
 
nolo contendere
Guest
Posts: n/a
 
      03-25-2008
On Mar 25, 1:14*pm, Frank Seitz <devnull4...@web.de> wrote:
> Ben Morrow wrote:
> > Quoth Ben Bullock <benkasminbull...@gmail.com>:
> >>On Tue, 25 Mar 2008 11:59:23 +0000, John W. Krahn wrote:
> >>>Ben Bullock wrote:

>
> >>>>The foreach version seems to first read the whole of the file into an
> >>>>array, and then go through it line by line:

>
> >>>perldoc -q "What is the difference between a list and an array"

>
> Hm. Why is this distinction relevant here?
>
> > A list is immutable *after it has been
> > created*. Obviously you can create lists with any contents, otherwise
> > you would be limited to using only lists compiled into perl. foreach
> > accepts a list as argument and iterates over it;

>
> use strict;
> use warnings;
>
> my @a = qw/a b c/;
> for my $v (@a) {
> * * push @a,'d' if $v eq 'c';
> * * print "$v\n";}
>
> __END__
> a
> b
> c
> d
>


http://groups.google.com/group/comp....2ebca559578bcf
 
Reply With Quote
 
nolo contendere
Guest
Posts: n/a
 
      03-25-2008
On Mar 25, 1:50*pm, nolo contendere <simon.c...@fmr.com> wrote:
> On Mar 25, 1:14*pm, Frank Seitz <devnull4...@web.de> wrote:
>
>
>
> > Ben Morrow wrote:
> > > Quoth Ben Bullock <benkasminbull...@gmail.com>:
> > >>On Tue, 25 Mar 2008 11:59:23 +0000, John W. Krahn wrote:
> > >>>Ben Bullock wrote:

>
> > >>>>The foreach version seems to first read the whole of the file into an
> > >>>>array, and then go through it line by line:

>
> > >>>perldoc -q "What is the difference between a list and an array"

>
> > Hm. Why is this distinction relevant here?

>
> > > A list is immutable *after it has been
> > > created*. Obviously you can create lists with any contents, otherwise
> > > you would be limited to using only lists compiled into perl. foreach
> > > accepts a list as argument and iterates over it;

>
> > use strict;
> > use warnings;

>
> > my @a = qw/a b c/;
> > for my $v (@a) {
> > * * push @a,'d' if $v eq 'c';
> > * * print "$v\n";}

>
> > __END__
> > a
> > b
> > c
> > d

>
> http://groups.google.com/group/comp....2ebca559578bcf


also, see 'Perl: modifying an array in a loop'

http://blog.plover.com/prog/perl/undefined.html#3
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
compiling python 3.1.2 with local readline fails to get readline - help! gavino Python 4 09-20-2010 05:17 AM
How to end TCP socket data while using readline()? Arjun Python 2 02-27-2010 04:11 AM
Getting application ReadLine and Perl debugger ReadLine to cooperate Andrew DeFaria Perl Misc 1 01-30-2008 11:46 PM
Readline::readline() blocking all other threads Jean-Michel Ruby 0 12-22-2007 01:00 AM
Why no "perldoc -f for/foreach/while"? usenet@DavidFilmer.com Perl Misc 11 02-15-2006 11:28 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57