develooper Front page | perl.perl5.porters | Postings from June 2010

Re: [perl #75596] Iterator not reset on array assignment

Thread Previous | Thread Next
From:
Eric Brine
Date:
June 8, 2010 12:53
Subject:
Re: [perl #75596] Iterator not reset on array assignment
Message ID:
AANLkTil6N6pGZye8ENSMFDGs4N8bmGisTTjPAxhSq3aG@mail.gmail.com
On Mon, Jun 7, 2010 at 5:27 PM, perl@profvince.com <
perlbug-followup@perl.org> wrote:

> # New Ticket Created by  perl@profvince.com
> # Please include the string:  [perl #75596]
> # in the subject line of all future correspondence about this issue.
> # <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=75596 >
>
>
> This is a bug report for perl from perl@profvince.com,
> generated with the help of perlbug 1.39 running under perl 5.10.1.
>
>
> -----------------------------------------------------------------
> [Please describe your issue here]
>
> Perl doesn't reset the array iterator when the array is cleared :
>
>    $ perl5.12.1-64 -M5.012 -E 'my @a = "a" .. "c"; my ($i, $v) = each @a;
> say "$i: $v"; @a = "A" .. "C"; ($i, $v) = each @a; say "$i: $v"'
>    0: a
>    1: B
>

The iterator can be reset by calling keys(). This is efficient in void
context.

$ perl -E'
    my @a = "a".."c";
    for (1..2) {
        my ($i,$v) = each @a;
        say "$i: $v";
        @a = "A".."C";
    }
'
0: a
1: B

$ perl -E'
    my @a = "a".."c";
    for (1..2) {
        keys @a;
        my ($i,$v) = each @a;
        say "$i: $v";
        @a = "A".."C";
    }
'
0: a
0: A

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About