in reply to pruning of empty arrayrefs in nested array
The likely cause of your problems is that (despite the name) delete on an array element doesn't completely remove that element from the array, it only sets it to be undef.
The simplest way to completely remove unwanted from elements from an array is to use grep: @array = grep{ <test for wanted> } @array;
But as you want to do it recursively, something like this might work for your problem (untested!):
sub prune { my $aref = shift; @{ $aref } = map { if( ref() eq 'ARRAY' ) { ## an arrayref? if( @{ $_ ) { ## Has values, recurse and return the prune +d ref (or nothing if it was empty after pruning). prune( $_ ); } else { ## return nothing (); } } else { ## not an array ref, return it whatever it is $_; } } @{ $aref }; return @{ $aref } ? $aref : (); ## Still contains something, retur +n it; or nothing. } my $refWanted = prune( \@nested );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: pruning of empty arrayrefs in nested array
by Laurent_R (Canon) on Apr 26, 2015 at 21:07 UTC | |
by BrowserUk (Patriarch) on Apr 26, 2015 at 21:26 UTC |