in reply to Re: Interesting Use for "state" Variables
in thread Interesting Use for "state" Variables

Python has enumerate for this. For example:

for i, item in enumerate(array): do_something_with(item, i)
The nearest thing I can find to that in Perl is the List::MoreUtils pairwise function, something like:
my @idx = 0 .. $#array; pairwise { do_something_with($b, $a) } @idx, @array;
or perhaps the CPAN Array::Each::Override module.

Replies are listed 'Best First'.
Re^3: Interesting Use for "state" Variables
by BrowserUk (Patriarch) on Mar 10, 2009 at 20:23 UTC

    You can achieve something similar without modules a couple of ways:

    @a = 'A'..'J'; for my $r ( map { k=> $_, v=> $a[ $_ ] }, 0 .. $#a ) { print "@{ $r }{ k, v }\n" } 0 A 1 B 2 C 3 D 4 E 5 F 6 G 7 H 8 I 9 J

    Or

    @a = 'A'..'J'; for my $r ( map [ $_, $a[ $_ ] ], 0 .. $#a ) { print "$r->[0]:$r->[1]\n" } 0:A 1:B 2:C 3:D 4:E 5:F 6:G 7:H 8:I 9:J

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.