Loading ...
Sorry, an error occurred while loading the content.

Re: Stepping through an Array inside foreach

Expand Messages
  • Damien Carbery
    ... array when ... make a ... changes ... You can t look ahead or behind when using foreach. Use for instead. for ( my $i = 0; $i
    Message 1 of 2 , Oct 1, 2003
    • 0 Attachment
      --- In perl-beginner@yahoogroups.com, Electron One <electron1@q...> wrote:
      > How do you temporarily step through the next couple values in an
      array when
      > you are in a foreach loop? Is there a way to tell what index you are
      > currently at?
      >
      > For example, you have,
      >
      > foreach $Line (@File){
      > if($Line =~ /condition/){
      > #here you want to see what the next values in the array hold, to
      make a
      > decision
      > #you want to be able to go to those future or past indexes make
      changes
      > than jump out of if loop, and proceed as normal
      > #with index what it was before entered the if loop
      >
      >
      > }
      >
      >
      > }

      You can't look ahead or behind when using foreach. Use 'for' instead.

      for ( my $i = 0; $i < $#File; $i++ )
      {
      if ( $File[$i] =~ /condition/ )
      {
      # Be sure to check you don't look beyond
      # either end of the list.
      }
      }
    Your message has been successfully submitted and would be delivered to recipients shortly.