in reply to Construct for Skipping and Continue

The scalar range operator, or flip-flop, is handy for constructs like this:

my @arr = qw( bar qux foo qux2 bar2 foo foo3 ); my $marker = "foo"; for (@arr) { print $_, $/ if ($marker eq $_) .. 1; } __END__ foo qux2 bar2 foo foo3
That makes the decision to print be carried by the state of the range operator. A simpler loop results.

GrandFather++

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: Construct for Skipping and Continue
by GrandFather (Saint) on Feb 23, 2007 at 03:34 UTC

    Watch that .. 1 if you happen to be reading from a file at any point during the script. Interesting things could happen! From the perlop documentation for 'Range Operators':

    If either operand of scalar ``..'' is a constant expression, that operand is considered true if it is equal (==) to the current input line number (the $. variable).

    DWIM is Perl's answer to Gödel