Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

if block inside map

by mhearse (Chaplain)
on Nov 11, 2011 at 16:15 UTC ( #937607=perlquestion: print w/ replies, xml ) Need Help??
mhearse has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to figure out how to put an if block inside a map. Can someone give me an example?

Replies are listed 'Best First'.
Re: if block inside map
by choroba (Chancellor) on Nov 11, 2011 at 16:23 UTC
    @x = map { if ($_ > 1) { 1 } } (-5 .. 5)
    But trying to read your mind, I suppose you need rather
    @x = map { if ($_ > 1) { 1 } else { 0 } } (-5 .. 5)
    which can be also written with the ternary operator:
    @x = map { $_ > 1 ? 1 : 0 } (-5 .. 5)

      Just to elaborate a little.  You can also leave off the else branch, in which case map would return the empty string (not sure why not undef) when the if-condition isn't true:

      my @x = map { if ($_ > 0) { $_*2 } } (-2 .. 2); use Data::Dumper; print Dumper \@x; __END__ $VAR1 = [ '', '', '', 2, 4 ];

      When you don't want to return anything in those cases, you can use the empty list ():

      my @x = map { if ($_ > 0) { $_*2 } else { () } } (-2 .. 2); # or: my @x = map { $_ > 0 ? $_*2 : () } (-2 .. 2); use Data::Dumper; print Dumper \@x; __END__ $VAR1 = [ 2, 4 ];
      ouh man, you made my day with this. Specially with ternary operator

        Other ways:

        • If you can use empty strings instead of zeros in the output list (i.e., you're not doing arithmetic with the elements of the output),
          then either  map { $_ > 0 } or just  map $_ > 0, (note the terminating comma) will do the trick:
          c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @x = map $_ > 1, -3 .. 3; dd \@x; " ["", "", "", "", "", 1, 1]
        • If you gotta have 0s,  || or  or will do:
          c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @x = map { $_ > 1 or 0 } -3 .. 3; dd \@x; " [0, 0, 0, 0, 0, 1, 1]
          (Note that  map $_ > 1 || 0, -3 .. 3 would also work, but  map $_ > 1 or 0, -3 .. 3 will not. Can you say why? This is one reason why some Best Practices recommend avoiding the  map EXPR, LIST form of map.)
        • And of course, there are other ways...


        Give a man a fish:  <%-{-{-{-<

Re: if block inside map
by Corion (Pope) on Nov 11, 2011 at 16:22 UTC

    Have you looked at map? What parts of it do you have problems with? What else have you tried, and how did it fail?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://937607]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (11)
As of 2016-08-25 20:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The best thing I ever won in a lottery was:















    Results (362 votes). Check out past polls.