简体   繁体   English

如何获取Perl的grep返回的列表的引用?

[英]How to get a reference to the list returned by Perl's grep?

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. 我想使用grep函数来过滤值列表,并将对已过滤列表的引用存储到哈希中。 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: 只有当我尝试时, %$stuff哈希是:

$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 ]
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM