I like this sort of pattern (update: maybe "style" would be a better word to use here than "pattern"):
An if (COND) { ... } structure with its messy indentation and visual presentation is avoided, and cyclomatic complexity is reduced.c:\@Work\Perl\monks>perl -wMstrict -le "my @SlectedColumns = (9, 7, 6, 0, 5, 0, 4, 3, 2, 0, 1, 9, 9); my $end_index = 10; ;; COLUMN: for my $col (@SlectedColumns[1 .. $end_index]) { next COLUMN unless $col != 0; print qq{do something with col $col}; } " do something with col 7 do something with col 6 do something with col 5 do something with col 4 do something with col 3 do something with col 2 do something with col 1
Update 1: In my experience, this pattern turns up quite often in a context like:
use constant RX_COMMENT => qr{ ... }xms; ... ... sub is_valid { my ($string, ) = @_; return $string =~ RX_COMMENT ? 0 : $string =~ RX_BLANK_STRING ? 0 : $string =~ RX_VALID_LINE ? 1 : die "invalid: '$string'" # default ; } ... ... LINE: while (my $line = <$fh>) { next LINE unless is_valid($line); do_something_with_known_valid($line); do_something_else_with($line); ... } # end while LINE
Update 2: This is my 4000th writeup!
Give a man a fish: <%-{-{-{-<
In reply to Re: loop array excluding some elements
by AnomalousMonk
in thread loop array excluding some elements
by IB2017
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |