Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^4: iterating through array and saving matches to new array

by Marshall (Canon)
on Apr 19, 2018 at 23:05 UTC ( [id://1213203]=note: print w/replies, xml ) Need Help??


in reply to Re^3: iterating through array and saving matches to new array
in thread iterating through array and saving matches to new array

Yes, actually it would be. Although I would still prefer the block form of grep.
I show how to use map{}

It actually took me quite a while to learn how to return "nothing" instead of "undef" from a map.

  • Comment on Re^4: iterating through array and saving matches to new array

Replies are listed 'Best First'.
Re^5: iterating through array and saving matches to new array
by AnomalousMonk (Archbishop) on Apr 20, 2018 at 01:29 UTC
    ... how to return "nothing" instead of "undef" from a map.

    That is a bit tricky :)


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

      I use grep to get a subset of an array.
      I use map to transform an array into another array.
      Special syntax for map can be used to do both, in certain situations.

      UPDATE: I have now forgotten the exact code that I used to "return nothing", but something like this (untested) should work:

      map{some condition ? $val : ()}@array;
      This () is not "undefined", it is absolutely nothing. Of course it it possible to combine map and grep to filter out undefined values. I remember doing this for some reason, but it's been a few years. If someone wants to do this, just knowing that it is possible will get you started. perl doc map for more info.

        c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @ra = (1 .. 5); ;; my @rb = map { $_ == 3 ? () : $_ } @ra; dd \@rb; " [1, 2, 4, 5]


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1213203]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-04-18 14:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found