<?xml version="1.0" encoding="windows-1252"?>
<node id="965000" title="Re^2: Perlplexation - foreach shoulda Known" created="2012-04-13 17:51:18" updated="2012-04-13 17:51:18">
<type id="11">
note</type>
<author id="176576">
eyepopslikeamosquito</author>
<data>
<field name="doctext">
&lt;P&gt;
&lt;blockquote&gt;
&lt;I&gt;
Is there a special variable that tracks what $i is tracking in that example?
&lt;/I&gt;
&lt;/blockquote&gt;
Curiously, this is very easy in Python:
&lt;CODE&gt;
x = [ 'apple', 'banana', 'orange' ]
for i, val in enumerate(x): print i, val
&lt;/CODE&gt;
which prints:
&lt;CODE&gt;
0 apple
1 banana
2 orange
&lt;/CODE&gt;
and fairly easy in Ruby:
&lt;CODE&gt;
x = [ 'apple', 'banana', 'orange' ]
x.each_with_index { |val, i| print "#{i} #{val}\n" }
&lt;/CODE&gt;
and I'm sure (need to wait for [moritz] to show me how) it's
easy in Perl 6 too (probably via Array &lt;C&gt;kv&lt;/C&gt; and/or &lt;C&gt;pairs&lt;/C&gt; methods?).
&lt;/P&gt;

&lt;P&gt;
I was hoping [doc://List::Util] or [cpan://List::MoreUtils]
might have something nice, but the best I could find is
to use an iterator like so:
&lt;CODE&gt;
use List::MoreUtils qw(each_array);
my @x = ( 'apple', 'banana', 'orange' );
my $it = each_array( @{[0..$#x]}, @x );
while ( my ($i, $val) = $it-&gt;() ) {
    print "$i $val\n";
}
&lt;/CODE&gt;
which is horrific.
Is there a better way in [doc://List::Util] or [cpan://List::MoreUtils]
that I missed?
&lt;/P&gt;

&lt;P&gt;
While I was writing this, [chromatic] showed how
to do it in Perl 5.12 or above:
&lt;CODE&gt;
my @x = ( 'apple', 'banana', 'orange' );
while ( my ($i, $val) = each @x ) {
    print "$i $val\n";
}
&lt;/CODE&gt;
&lt;/P&gt;
</field>
<field name="root_node">
964975</field>
<field name="parent_node">
964980</field>
</data>
</node>
