Date: Sun, 09 Sep 2001 21:25:23 GMT From: Bart Lateur Subject: Re: printing all even || odd numbers from array Message-Id: GunneR wrote: >$b = 0; > >foreach (@array1) { > if (($array1[$b] % 2) == 0) { > print "$array1[$b]\n"; > $b++; > next; > } > else { $b++; next; > } >} Even with such an awkward loop, you can still improve on the neatness of the code: $b = 0; foreach (@array1) { if (($array1[$b] % 2) == 0) { print "$array1[$b]\n"; } } continue { $b++; } but making $b the loop variable sounds like a better idea: foreach my $b (0 .. $#array1) { if (($array1[$b] % 2) == 0) { print "$array1[$b]\n"; } } Well alright, the perlish way would be to recognise the fact that in such a foreach loop, the loop variable gets set to an alias of each item in turn. The default loop variable is $_. So: $b = 0; foreach (@array1) { if ($_ % 2) == 0) { print "$_\n"; } } continue { $b++; } (Perhaps you still have a use for that $b?) -- Bart.