删除数组的Perl哈希中的空字符串

chr*_*ian 2 perl hash

我有这种结构的哈希

{
  a => ['1', '2', '', '5', '6', '7', '8'],
  b => ['1', '2', '3', '6', '7', '8', '9'],
  c => ['2', '', '4', '', '', '8', ''],
}
Run Code Online (Sandbox Code Playgroud)

我需要这样的一些

{
  c => ['2', '4', '8'], 
  a => ['1', '2', '5', '6', '7', '8'], 
  b => ['1', '2', '3', '6', '7', '8', '9']
}
Run Code Online (Sandbox Code Playgroud)

如何删除''值?

Сух*_*й27 11

您可以直接更改返回的数组引用列表,valuesperldoc中所述.

use warnings;
my %h = (
  'c' => ['2','','4','','','8',''],
  'a' => ['1','2','','5','6','7','8'],
  'b' => ['1','2','3','6','7','8','9']
);

@$_ = grep defined && length, @$_ for values %h;
Run Code Online (Sandbox Code Playgroud)

如果undef不应过滤掉值,

@$_ = grep !defined || length, @$_ for values %h;
Run Code Online (Sandbox Code Playgroud)