in reply to Trouble with loops

If you just want to test and remove the first element of an array then you don't need a loop:

my @array = qw( joby andy ben tom bob ); print "start\n"; for my $name ( @array ) { print "$name\n"; } print "\nmiddle\n"; shift @array and print "deleted\n" if $array[ 0 ] eq 'joby'; print "\nend\n"; for my $name ( @array ) { print "$name\n"; }

Replies are listed 'Best First'.
Re^2: Trouble with loops
by Narveson (Chaplain) on Jul 01, 2008 at 06:46 UTC
    you don't need a loop

    I guess the program doesn't need to loop through an array if the program author has already looked the array over and seen that the stated criterion only applies to the first element. By the same reasoning, you don't need shift:

    my @array = qw( joby andy ben tom bob ); print "start\n"; for my $name ( @array ) { print "$name\n"; } print "\nmiddle\n"; print "deleted the first element because it was 'joby'\n"; @array = qw( andy ben tom bob ); print "\nend\n"; for my $name ( @array ) { print "$name\n"; }