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

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

by AnomalousMonk (Archbishop)
on Apr 19, 2018 at 21:32 UTC ( [id://1213196]=note: print w/replies, xml ) Need Help??


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

Wouldn't the code be more readable/maintainable/etc with List::Util::uniq():
    my @newb = uniq grep $_ > 850, @b, @c, @d;
Results are the same per Test::More::is_deeply(). (In older Perls, see List::MoreUtils::uniq.)


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

Replies are listed 'Best First'.
Re^4: iterating through array and saving matches to new array
by Marshall (Canon) on Apr 19, 2018 at 23:05 UTC
    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.

      ... 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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-24 17:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found