flies has asked for the wisdom of the Perl Monks concerning the following question:

i want to use a statistics::basic::computedvector::set_filter, but the filter i want to use is an array of 1's and 0's. is this possible?

here's what I want to do:

my $v1 = vector(1,2,3,4,5); my @filter = qw(1 0 0 1 1); my $cv = computed($v1); $cv->set_filter(@filter);

I tried the following:

my $v1 = vector(1,2,3,4,5); my $compV1 = computed($v1); my @filter = (1) x $compV1->query_size(); $filter[-1] = 0; say $compV1; $compV1-> set_filter( sub { grep { $filter[$_] } 0..$#_ } ); say $compV1;

but this prints

[1, 2, 3, 5, 7] [0, 1, 2, 3]
what should I do?

Replies are listed 'Best First'.
Re: statistics basic - filter vector according to array
by ikegami (Patriarch) on Mar 09, 2011 at 16:47 UTC
    ->set_filter( sub { map $_[$_], grep $filter[$_], 0..$#_ } )

    or

    ->set_filter( sub { map { $_[$_] ? $filter[$_] : () } 0..$#_ } )

    or

    ->set_filter( sub { @_[ grep $filter[$_], 0..$#_ ] } )

    Update: Fixed typo in first solution.

      thanks. these all solve the problem and help me know more about map and grep.