Re: Readline using foreach and while
- From: "szr" <szrRE@xxxxxxxxxxxxxxx>
- Date: Wed, 26 Mar 2008 21:45:32 -0700
nolo contendere wrote:
On Mar 26, 10:19 am, "szr" <sz...@xxxxxxxxxxxxxxx> wrote:
nolo contendere wrote:
On Mar 26, 12:33 am, "szr" <sz...@xxxxxxxxxxxxxxx> wrote:
Ben Morrow wrote:
Quoth Frank Seitz <devnull4...@xxxxxx>:[...]
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.)
Provably untrue. See Ben's example. I'll restate the concept below.
my @ary = qw/a b c/;
# for (@ary, ()) {
# for ( (), @ary ) {
for ( @ary ) {
push @ary, 'd' if /c/;
print;
}
...
only the uncommented 'for' line prints a 'd' at the end. so what you
say MAY be true if LIST is ONLY an array.
Isn't that because the two commented one are two lists being combined
into a new list, and it's *that* new list that's being iterated over, so
even if you add to @ary, it doesn't change the "new list", which is just
that, a new list created at the start of the loop before iterating
begins - therefore the values of the new list are set and @ary has
nothing to do with it after the create of the "new list."
Isn't this correct?
--
szr
.
- Follow-Ups:
- Re: Readline using foreach and while
- From: Peter J. Holzer
- Re: Readline using foreach and while
- References:
- Readline using foreach and while
- From: Saurabh Jain
- Re: Readline using foreach and while
- From: Ben Bullock
- Re: Readline using foreach and while
- From: Ben Morrow
- Re: Readline using foreach and while
- From: Frank Seitz
- Re: Readline using foreach and while
- From: Ben Morrow
- Re: Readline using foreach and while
- From: szr
- Re: Readline using foreach and while
- From: nolo contendere
- Re: Readline using foreach and while
- From: szr
- Re: Readline using foreach and while
- From: nolo contendere
- Readline using foreach and while
- Prev by Date: Re: display in a tree structure
- Next by Date: need help on cgi to get multi pages and could sort by column from Oracle
- Previous by thread: Re: Readline using foreach and while
- Next by thread: Re: Readline using foreach and while
- Index(es):
Relevant Pages
|