in reply to Skipping Elements in an Array

#! /usr/bin/perl -w use strict; my @Array = ('AAA','BB','CCCC','DD','EEEEE','FFF','GGGG'); my $num_of_skip = 2; # Keep and increment your own index. for changes the loop var on you my $i = 0; while ($i < @Array) { # Adjust the index if necessary # Q: How can i make use of $num_of_skip here? A: Like this: $i += $num_of_skip + 1 if (length ($Array[$i]) == 2); # Notice we're post-incrementing the index here too print "$Array[$i++]\n"; }

Replies are listed 'Best First'.
Re^2: Skipping Elements in an Array
by gam3 (Curate) on Apr 07, 2005 at 13:33 UTC
    You need a next in your if block.
    if (length ($Array[$i]) == 2) { $i += $num_of_skip + 1; next; }
    This will do 2 things for you: it will keep you from printing an element were length == 2 and will keep you from having to worry about the post increment.

    -- gam3
    A picture is worth a thousand words, but takes 200K.