I can only think of setting a variable at the end of the foreach like

foreach my $file (@files) {
        ## do stuff
        
        my $last = $file;
        }

-----Original Message-----
From: Carl Rogers [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, February 06, 2002 4:40 PM
To: [EMAIL PROTECTED]
Subject: Accessing previous element of an array in a 'foreach' loop


Good day;

I'm using foreach to do something to an array. is there a way I can 
reference a previously 'seen' element without having to traverse the array 
with a for(;;) statement?

ie. I'd like this to work:

foreach(@array) {

        if ($_ =~/somecondition/) {
        # I want to do something to the element prior to $_ ($_ - 1)
        }
}

However, I know I can use

for($x=0, $x<$#array; $x++){
        if($array[$x] =~ /somecondition/){
        # do something to $array[$x-1]
        }

}

but I'm curious if there is a way to do the same in the foreach loop.

Thank you very much for your time.
Carl


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

----------------------------------------------------------------------------
--------------------
The views and opinions expressed in this email message are the sender's
own, and do not necessarily represent the views and opinions of Summit
Systems Inc.


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

Reply via email to