How to get a link to the list returned by Perl grep?

I would like to use the grep function to filter the list of values ​​and store the link to the filtered list in a hash. What I need is something like:

 my $allValues = [ 'a', 'unwanted value', 'b', 'c', ]; $stuff = { 'values' => grep { $_ ne 'unwanted value' } @$allValues }; 

Only when I try to do this, hash %$stuff :

  $ VAR1 = {
           'b' => 'c',
           'values' => 'a'
         };

Is there a way to fix the anonymous hash code so that I get:

  $ VAR1 = {
           'values' => [
                         'a',
                         'b',
                         'c'
                       ]
         };

But not by creating a local variable or calling a subroutine (in other words, everything on the line)?

+6
source share
1 answer

try:

 $stuff = { 'values' => [ grep { $_ ne 'unwanted value' } @$allValues ] }; 
+11
source

All Articles