I would like to use the grep
function to filter a list of values and store a reference to the filtered list into a hash. What I am after is something like:
my $allValues = [
'a',
'unwanted value',
'b',
'c',
];
$stuff = {
'values' => grep { $_ ne 'unwanted value' } @$allValues
};
Only, when I try that, the %$stuff
hash is:
$VAR1 = { 'b' => 'c', 'values' => 'a' };
Is there a way to fix the anonymous hash-creating code so that I instead get:
$VAR1 = { 'values' => [ 'a', 'b', 'c' ] };
But not by creating a local variable or calling a subroutine (in other words, all in-line)?
try with:
$stuff = {
'values' => [ grep { $_ ne 'unwanted value' } @$allValues ]
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With