Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: loop array excluding some elements

by Discipulus (Monsignor)
on Mar 08, 2018 at 18:15 UTC ( #1210524=note: print w/replies, xml ) Need Help??

in reply to loop array excluding some elements

Hello IB2017,

you are looking for wisdom, not for elegance! ;=)

Elegance can be your enemy: better looking for efficiency and readability.

You already received a wise answer by Eily++ about grep

If your do_something is short as print you can consider also the unless postscript form:

foreach my $ele (@SlectedColumns[1..$NrCoumns]) { do_something unless $ele == 0; }

It's an handy and readable syntax.

If the loop is complex and actions are more, something very similar to your example is a wise form to express some logic: a good principle exit loop early

foreach my $ele(@SlectedColumns[1..$NrCoumns]) { # exit soon next if $ele == 0; next unless defined $ele; .... }


There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1210524]
and the voices are still...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (14)
As of 2018-03-19 15:42 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (241 votes). Check out past polls.