Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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

by AnomalousMonk (Archbishop)
on Apr 20, 2018 at 01:29 UTC ( [id://1213212]=note: print w/replies, xml ) Need Help??


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

... how to return "nothing" instead of "undef" from a map.

That is a bit tricky :)


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

Replies are listed 'Best First'.
Re^6: iterating through array and saving matches to new array
by Marshall (Canon) on Apr 20, 2018 at 02:56 UTC
    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:  <%-{-{-{-<

        Same idea. The () is not "undef", it is no return value. In a foreach loop, a next statement might be used to skip processing of an input value.

Log In?
Username:
Password:

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

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

    No recent polls found