Re: Readline using foreach and while



On 2008-03-27 04:45, szr <szrRE@xxxxxxxxxxxxxxx> wrote:
nolo contendere wrote:
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."

Yes. But the same should be true for

for (@ary) {
...
}

for() expects a list, the list is constructed from the elements of @ary.
If you modify @ary after the list is constructed, the list shouldn't be
affected, but it is. I think Ben Morrow is right here: This smells like
an optimization: If there is only a single array, it can be used
directly instead of creating a list from it.

hp
.



Relevant Pages

  • Re: Accessing next/prev element while for looping
    ... > The python way is much more succinct. ... > previous or the next element in the array before continuing iterating. ... > incredibly silly given that python lists under the hood are linked ... the iterator used by the for loop, not the list it is iterating over. ...
    (comp.lang.python)
  • Re: Vector or List
    ... > I'm fairly new to the STL and i was wondering when was the best situations ... to elements other than iterating over them ... For lists, ... I'm Schobi at suespammers dot org ...
    (microsoft.public.vc.stl)
  • Re: Iterator Class?
    ... > there is a significant performace penalty when using getfor long Lists. ... > It appears as though getinsists on iterating from the beginning of the ... > list to arrive at the desired element. ... the profiler has never complained about iterating over a 10 ...
    (comp.lang.java.programmer)
  • Re: Iterator Class?
    ... > there is a significant performace penalty when using getfor long Lists. ... > It appears as though getinsists on iterating from the beginning of the ... > list to arrive at the desired element. ... the profiler has never complained about iterating over a 10 ...
    (comp.lang.java.help)
  • Re: variable declaration
    ... would forbid iterating through lists containing a mix of different types. ... you declare a variant type where the rules are relaxed *for that situation ... Thomas Bartkus ...
    (comp.lang.python)