in reply to search array for item, move to next item

You got lured into the idea that any array traversal should be done with for (@array), and that using a C-style for loop is a one way ticket to hell.

But in this case, using a C-style loop makes sense:

my $target_word; for (my $i = 0; $i < @arglist; ++$i) { $target_word = $arglist[$i+1], last if $arglist[$i] eq "-iL"; }
Note that if "-iL" is the last word, undef is assigned to $target_word, which effectively is a no-op.

Replies are listed 'Best First'.
Re^2: search array for item, move to next item
by Fletch (Bishop) on Oct 17, 2008 at 14:33 UTC

    Actually it's trivial to still use an iterator-style for loop, you just have to pick something slightly different to iterate over.

    my $target_word; for my $idx ( 0..($#arglist-1) ) { if( $arglist[ $idx ] eq '-iL' ) { $target_word = $arglist[ $idx + 1 ]; last; } }

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.