I have an array of hashes like so:
my @array = [
{1 => "Test1"},
{},
{1 => "Test2"},
];
I try to filter out the {} from this array so that the end result is an array without any {} like so:
my @array = [
{1 => "Test1"},
{1 => "Test2"},
];
I tried using something like this but it doesn’t work:
my @new_array = grep(!/{}/, @array);
The
grepcommand can take a regex as argument, but also an arbitrary block of code. And please do not try to match Perl code with regexes, this doesn’t work.Instead, we ask for the number of keys in the anonymous hash:
This selects all hashes in
@arraywhere the number of keys is not zero.