Click here to get back home

Readline using foreach and while

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Readline using foreach and while Saurabh Jain 03-25-2008
Posted by nolo contendere on March 25, 2008, 1:55 pm
Please log in for more thread options
>
>
>
> > Ben Morrow wrote:
> > >>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 a=
n
> > >>>>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 =3D qw/a b c/;
> > for my $v (@a) {
> > =A0 =A0 push @a,'d' if $v eq 'c';
> > =A0 =A0 print "$v\n";}
>
> > __END__
> > a
> > b
> > c
> > d
>
> http://groups.google.com/group/comp.lang.perl.misc/msg/9f2ebca559578bcf

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

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

Posted by John W. Krahn on March 25, 2008, 4:56 pm
Please log in for more thread options
Frank Seitz wrote:
> Ben Morrow wrote:
>>> 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) {

We were talking about using a *list* in a foreach loop so that should be:

for my $v ( qw/a b c/ ) {

> push @a,'d' if $v eq 'c';

You are modifying an *array*, not a list.

> print "$v\n";
> }
> __END__
> a
> b
> c
> d


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

Posted by Ben Bullock on March 25, 2008, 6:33 pm
Please log in for more thread options

> We were talking about using a *list* in a foreach loop

You might have been, but I wasn't. I was talking about Perl reading in
data from a file.


Posted by Ben Bullock on March 25, 2008, 6:32 pm
Please log in for more thread options
>
>
>
> > 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


Posted by Ben Bullock on March 25, 2008, 7:02 pm
Please log in for more thread options

> 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.

If we can create lists, then there must be a data structures inside
Perl which represents those created lists. Let's call it the "newly-
created list" data structure. There is also a data structure for
arrays, of course.

Can you tell me the difference between the "newly-created lists" data
structure and the array data structure? Is there an example of Perl
code where we can see how the "newly-created lists" differ from
arrays? Or are these actually the same thing in practice?

> 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

But I wasn't talking about modifying the list after that.

: for an example where you
> can, see Tie::File, which reads a file into an *array* instead.

OK:

http://perldoc.perl.org/Tie/File.html

But it says very shortly into the documentation "The file is not
loaded into memory" and "Changes to the array are reflected in the
file immediately". So your statement about "reads a file into an
array" is wrong. It is not reading the file into an array. It is
making a file appear to be an array. To read a file into an array use:

my @ary = <handle>;

Then one can loop over the array using

foreach (@ary)

Similar ThreadsPosted
Asynchronous readline? September 6, 2004, 9:27 am
Term::ReadLine::GNU problem January 1, 2006, 11:57 am
ReadKey/ ReadLine query July 4, 2006, 2:40 am
readline - possible security hole March 30, 2007, 8:50 am
circular references, chdir() and Term::ReadLine::Gnu? February 7, 2006, 8:09 pm
Strange characters using Term::Readline on Win32 July 28, 2006, 9:05 am
HELP: readline() on unopened filehandle DATA at G:/PERLLIB/LIB/site_perl/5.8.2/MIME/WordDecoder.pm line 579. May 21, 2006, 10:59 am
foreach vs. for October 24, 2004, 6:18 pm
foreach in my May 18, 2005, 10:40 am
Using foreach?? maybe.. June 3, 2007, 12:30 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap