I don't know if you can do it using a foreach but I would use a for loop.
It may be just a tad slower but you'll get the results you're looking for.
Using the loop below will start at the last index of the array @a and loop
down to the first (assuming that the first index of the array is 0).  

@a = ("a","b","c","d","e");

for ($b=$#a; $b > -1; $b--)
{
    print "$b $a[$b]\n";
};


In the loop you showed using the $index counter, the $index would not be the
actual index of the item in the array but the current index of the loop.

Maybe one of the more experienced people on the list can offer you a way of
doing it using a foreach loop that might be faster.

Hope this helps,
Chris



-----Original Message-----
From: matthschulz [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, August 14, 2001 6:40 PM
To: [EMAIL PROTECTED]
Subject: index inside foreach{ ..}


I have read something but could not find an answer:
I had to do a foreach like this:

A------------------------------------
@a = ("a","b","c","d","e");

foreach $b (reverse @a) {
    print "$b\n";
};

Now i needed to access the index inside the foreach to do:
B------------------------------------
@a = ("a","b","c","d","e");

foreach $b (reverse @a) {
    print "$index $b\n";
};

which i did with a additional variable
C------------------------------------
@a = ("a","b","c","d","e");
$index =0;
foreach $b (reverse @a) {
    $index++;
    print "$index $b\n";
};

Is there a way to acces the internal index of the foreach loop as $index in 
example "B" ?

Thanks in advance 
Matth

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to