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

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.

Replies are listed 'Best First'.
Re^7: iterating through array and saving matches to new array
by AnomalousMonk (Archbishop) on Apr 21, 2018 at 20:16 UTC

    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.

        Exactly the same idea. I just meant to supply a worked example of something you had discussed in pseudo-ish code :)


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