Re: Readline using foreach and while



Ben Morrow wrote:
Quoth "szr" <szrRE@xxxxxxxxxxxxxxx>:

Actually the behaviors of "for (@ary)" and "for (@ary, ())" do seem
consistant if you really think about it. The resulting list is what
it iterates over (from the first element, to what ever *count* is...
in the former case *count* come fro mthe array, and since the
condition is checked at the start of each iteration, if the array is
added to, the count is incremented.

In the latter case, a new list is created from contents of @ary + an
empty list, which gives you a new list, which contains the values of
@ary, but is a new seperate list, and thus is not effected by
changes to @ary because it has it's own copy of @ary's values.

OK, now explain to me why

my @ary = qw/a b c/;
print map { /c/ and push @ary, 'd'; $_ } @ary;

*doesn't* work like that :).

Actually it does. The difference is, map doesn't recheck the count every
time around like for/foreach do. If you print the contents of @ary after
the line with the map statement, it does indeed contain 'd' at the end.
This behavior seems to correct, as one would likely expect that the list
map returns when it is finished to be the same length as the one
/passed/ into map at the start. If you pass a 3 element list, you should
get back a 3 element list, should you not? Or do you consider this to be
a bug?

However, consider:

my @ary = qw/a b c/;
print map { /c/ and $_ = 'd'; $_ } @ary;


__OUTPUT__
abd

$_ is aliased to the current array element just like in for. Again, the
only difference I see if that map doesn't recheck the count of the
passed list for each iteration. Well, that and map returns a list :-)

To me, this behavior is part of what separates map from for/foreach.

--
szr


.



Relevant Pages

  • Continuous iterations of fractals
    ... Like many others, I was intrigued when I first saw pictures of Julia sets and Mandelbrod sets, and I had lots of fun making generating them on a computer. ... To get a continuous time-evolution, we need the "Nth iterative root" of this map, a map f, such that: ... has as Nth root ... he either reverse-iterates a point a number of times by using the inverse iteration: ...
    (sci.physics.research)
  • Re: Readline using foreach and while
    ... condition is checked at the start of each iteration, if the array ... map doesn't recheck the count ... the passed list for each iteration. ... @ary, which comes from a scope outside this sub, now contains (h, a, b, ...
    (comp.lang.perl.misc)
  • Re: Readline using foreach and while
    ... condition is checked at the start of each iteration, if the array is ... the line with the map statement, it does indeed contain 'd' at the end. ... passed list for each iteration. ...
    (comp.lang.perl.misc)
  • Re: "Higher order" bifurcation of discrete map
    ... It is just a discrete iteration, ... the nature of the intended map, ... If you want to transform this map into a "real" trifurcation, ... projection through Re or Im of a complex map, the answer is I don't know. ...
    (sci.math)
  • Re: [Map]modifying a map through an iteration
    ... that within the loop modifying values attached to a key is not a problem ... and the Map itself is unchanged. ... items when through iteration you want to remove an item other than the one ... public class MapTest { ...
    (comp.lang.java.help)