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 ]
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.