Re: Readline using foreach and while



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


.



Relevant Pages

  • Re: iterating bit-by-bit across int?
    ... > function that mutates an integer by iterating across the bits, ... > lists of bools; and then I can iterate through that, ... array module, convert your integer to an array, do a bunch of bit ...
    (comp.lang.python)
  • Re: noob request for comments - NFA simulator
    ... Pushing a result state onto nil naturally ... couldn't work out what the push was for. ... with mapcan requiring freshly consed lists? ... > abstraction I have to follow to get back to the original transition ...
    (comp.lang.lisp)
  • Re: HTML::Treebuilder and nested elements (alternates to look_up/look_down?)
    ... sub find_firstlevel_uls ... push @found, ... # Extract nested html bullet lists, ... # get a list of LIs, avoiding nested LIs as seperate entries ...
    (comp.lang.perl.modules)
  • Re: Changing value of a list variable passed as argument to function
    ... that variable changed in a way 'push' macro would change it? ... the type LIST is a hack: (OR CONS NULL) ... You can also use arrays, symbols, cons cells, clos objects, etc ...
    (comp.lang.lisp)
  • Re: F# more popular than Common Lisp
    ... functions: pop and push. ... Dynamic typed languages ask the object how big it is ... In statically strongly typed languages such as Standard ML, lists ... typically are monomorphic (can only store elements of a particular type, ...
    (comp.lang.lisp)