hubb0r has asked for the wisdom of the Perl Monks concerning the following question:
I have a general question about foreach loops. Usually I like to loop through things with the perlish:
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:
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.
Thanks!!
foreach my $line (@arr) { ## stuff goes here; }
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:
for (my $x = 0; $x < $#arr; $x++) { my $line = $arr[$x]; ## stuff goes here; }
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.
Thanks!!
Back to
Seekers of Perl Wisdom