in reply to Boolean array indexing

If your arrays are small go with one of the many grep/map/slice answers offered, but be aware that for large arrays they will create two or more huge intermediate lists that consume substantial amounts of memory, and thus time.

The alternative that avoids those huge stack allocations only requires a single temporary scalar and runs very efficiently:

@a=(6,7,8); @b=(3,2,1);; $i=0; $b[$_] > 1 and $c[ $i++ ] = $a[$_] for 0 .. $#a;; print @c;; 6 7

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Boolean array indexing
by Anonymous Monk on Nov 22, 2013 at 07:11 UTC
    Why not use push though?
    $b[$_] > 1 and push(@c, $a[$_]) for 0 .. $#a;;

      Good point. Does away with the need for a temp var. ++


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.