<?xml version="1.0" encoding="windows-1252"?>
<node id="439714" title="Foreach Loops" created="2005-03-15 13:56:59" updated="2005-08-15 09:02:22">
<type id="115">
perlquestion</type>
<author id="378734">
hubb0r</author>
<data>
<field name="doctext">
I have a general question about foreach loops.  Usually I like to loop through things with the perlish:
&lt;br&gt;
&lt;code&gt;
foreach my $line (@arr) {
   ## stuff goes here;
}
&lt;/code&gt;
&lt;br&gt;
Occasionally I need to access the array element prior to the current one that I am on based on what I see in the current element.  When this is the case, I will usually fall back to the c-style:
&lt;br&gt;
&lt;code&gt;
for (my $x = 0; $x &lt; $#arr; $x++) {
    my $line = $arr[$x];
    ##  stuff goes here;
}
&lt;/code&gt;
&lt;br&gt;
Which obviously does allow me to get to where I need to be.  My question is, is there a way to access the index of the current element in a "perlish" foreach loop, so that I don't need to go the c-style way?  I much prefer the cleaner interface on the perl way of doing things.
&lt;br&gt;&lt;br&gt;
Thanks!!</field>
</data>
</node>
