Re: Readline using foreach and while



Peter J. Holzer wrote:
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.

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.

--
szr


.



Relevant Pages

  • Re: garbage collection problem in large linked lists
    ... Instead all buckets are allocated at once in an array. ... VG.net, which must be very scalable, but only for lists which would normally ... Linked lists require pointer dereferencing in order to traverse. ... When you run out of space, you allocate a new smaller array. ...
    (microsoft.public.dotnet.framework.performance)
  • Re: Dict sharing vs. duplication
    ... array to enforce unique keys and I use lists to enforce order. ... API in a page or two of Tcl code, it isn't so much a missing feature ... OpenACS database API which creates a new specialized data structure, ...
    (comp.lang.tcl)
  • Re: Translating python to scheme
    ... Scheme has mutation, and there are reasons to use it. ... confusing when considering a 2d array. ... in other languages: Lisp Lists and Vectors. ...
    (comp.lang.scheme)
  • Re: array in hash
    ... The important thing to remember here is that Perl lists and hashes are inherently one dimensional. ... Type of arg 1 to push must be array (not hash element) at hash.pl line 1, ...
    (comp.lang.perl.misc)
  • Re: vasam
    ... // Set the size for long lists in records #2 and #3 only! ... Here is the sequel of my previous post "Variable array sizes as members" ... I was given some advice on how to use malloc and realloc in oder to achieve ...
    (microsoft.public.vc.language)