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

[email protected] (Eric Brine)
Newsgroups perl.perl5.porters
Message-ID <[email protected]>
On Mon, Jun 7, 2010 at 5:27 PM, [email protected] <
[email protected]> wrote:

> # New Ticket Created by  [email protected]
> # 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 [email protected],
> 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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.