in reply to Foreach syntax

You could use grep:
my %is_available = ( zero => 0, one => 1, two => 2 ); for my $p9 ( grep { $is_available{$_} } keys %is_available ) { print $p9; }


Unless I state otherwise, all my code runs with strict and warnings

Replies are listed 'Best First'.
Re^2: Foreach syntax
by blazar (Canon) on Jul 26, 2008 at 08:03 UTC

    I personally believe that for the benefit of the OP it would be sensible to explain that while this does exactly what he asked for: namely "select" non zero values within the C<for> clause, it would be "inefficient" (in some sense) in that grep is a loop in disguise. Thus with such code one is iterating twice over... well, not just the same list, but two strictly correlated ones. Literally, it's like one had been doing:

    my @nonzero; for (keys %is_available) { push @nonzero, $_ if $is_available{$_}; } for my $p9 ( @nonzero ) { print $p9; }
    --
    If you can't understand the incipit, then please check the IPB Campaign.