Lets say I have the following Perl hash:
%hash = (
'A' => {
'B' => ['C', 'D', 'E'],
'F' => { 'G' => [], 'H' => [] },
'I' => []
} );
and I’d like to get rid of the []‘s to get the hash result below:
%hash = (
'A' => [
'B' => ['C', 'D', 'E'],
'F' => [ 'G', 'H', 'I' ]
]
)
(I hope I got my {} and [] balanced, my apologies if not, but) essentially I’d like to make it so that no empty arrays/ref’s exist. I’m sure this is possible/simple, but I’m not sure whether delete() will work, or if there’s a better method or a Perl module out there. Can someone steer me in the right direction?
It appears like your data might be nested arbitrarily, and you want to walk through it recursively, rewriting certain patterns to others. For that, I’d be using
Data::Visitor.This just illustrates the basic approach I’d use, and happens to work for the example input you gave. It might need various tweaks depending on what you want to do in the corner-cases pointed out in the other comments.