Remove empty strings in Perl hash of arrays

hash, perl

Solution

You can directly change list of array references returned by `values` as described in perldoc.

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;

and in the case that `undef` values should not be filtered out,

@$_ = grep !defined || length, @$_ for values %h;

Problem

I have a hash with this structure ``` { a => ['1', '2', '', '5', '6', '7', '8'], b => ['1', '2', '3', '6', '7', '8', '9'], c => ['2', '', '4', '', '', '8', ''], } ``` And I need some like this ``` { c => ['2', '4', '8'], a => ['1', '2', '5', '6', '7', '8'], b => ['1', '2', '3', '6', '7', '8', '9'] } ``` How can I delete the `''` values?

Original source