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.