|
Posted by Frank Seitz on March 25, 2008, 1:14 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 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
Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/ Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel
|
|
Posted by Ben Morrow on March 25, 2008, 1:49 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 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
|
|
Posted by szr on March 26, 2008, 12:33 am
Please log in for more thread options Ben Morrow wrote:
[...]
>>
>> 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...
Nothing really weird about it. In that case above, it's no different
than:
for (my $i=0; $i<@a; $i++) {
my $v = $a[$i];
push @a,'d' if $v eq 'c';
print "$v\n";
}
In either case, it's going over the array, one element at a time, in
sequence, s oif you "push" something onto the end, it grows the array by
one and thus the for look keeps going.
--
szr
|
|
Posted by nolo contendere on March 26, 2008, 9:58 am
Please log in for more thread options > Ben Morrow wrote:
> [...]
>
> >> 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";
> >> }
>
> > Good point. for is a little weird in this respect...
>
> Nothing really weird about it. In that case above, it's no different
> than:
>
> for (my $i=3D0; $i<@a; $i++) {
> =A0 =A0my $v =3D $a[$i];
> =A0 =A0push @a,'d' if $v eq 'c';
> =A0 =A0print "$v\n";
>
> }
>
> In either case, it's going over the array, one element at a time, in
> sequence, s oif you "push" something onto the end, it grows the array by
> one and thus the for look keeps going.
>
I think the 'weirdness' stems from the notion that 'for' supposedly
builds up a list prior to iterating over it, as opposed to 'while'
which (with respect to filehandles at least) does not. I'm not well-
versed enough to analyze the internals of how 'for' is implemented to
understand what exactly happens if LIST is simply a list, or an array,
or part of each, or if something gets appended, prepended, spliced to
an array that is part of LIST. Based on prior threads, it appears that
the behavior varies. Since the behavior is not really consistent (and
not really documented), it can validly be labeled as 'weird'.
|
|
Posted by szr on March 26, 2008, 10:19 am
Please log in for more thread options nolo contendere wrote:
>> Ben Morrow wrote:
>> [...]
>>
>>>> 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...
>>
>> Nothing really weird about it. In that case above, it's no different
>> than:
>>
>> for (my $i=0; $i<@a; $i++) {
>> my $v = $a[$i];
>> push @a,'d' if $v eq 'c';
>> print "$v\n";
>>
>> }
>>
>> In either case, it's going over the array, one element at a time, in
>> sequence, s oif you "push" something onto the end, it grows the
>> array by one and thus the for look keeps going.
>>
>
> I think the 'weirdness' stems from the notion that 'for' supposedly
> builds up a list prior to iterating over it,
Exactly. It builds a list, when necessary (like when you
use for (<FH>) { ... } ), unless it's alreayd there ( like
with for my $element (@array) { ... } ) and /THEN/ iterates over it.
It just keeps going until the list is expended. Adding to the array like
in the quoted examples above makes the list longer and hence the extra
iteration (since the length of the list is checked each time through the
loop.)
--
szr
|
| Similar Threads | Posted | | 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 |
|