Dear Monks, I am having difficulties in pruning some empty array references from a nested array. I have tried several strategies with no success. Here below I am going to paste some examples of my trials. It seems to me that the problems here are two: how to check the references for emptiness and how to delete them if they are empty. Thank you in advance. Best regards Luca
use Data::Dumper; use feature "say"; no strict; no warnings; use Scalar::Util 'reftype'; use Array::DeepUtils qw/:all/; sub prune1 { my $count = 0; my @arr = @{ $_[0] }; my $counter = 0; foreach ( @arr ) { if ( ref ( $_ ) ) { if ( not ( @{ $_[0] } ) ) { delete ( $arr[$counter] ); } else { prune1( $_ ); } $counter++; } } return ( \@{$_[0]} ); } sub prune2 { my $node = $_[0]; if ( ref( $node ) ) { for ( my $i = $#$node; $i >= 0; $i-- ) { my $subnode = $node->[$i]; if ( ref( $subnode ) and ( @$subnode == 0 ) ) { delete $node->[$i]; } else { prune2( $subnode ); } } } return ( $_[0] ); } sub prune3 { my ( $node ) = @_; my $type = reftype $node // ''; if ( $type eq 'ARRAY' ) { for ( my $i = $#$node; $i >= 0; $i-- ) { my $subnode = $node->[$i]; my $subtype = reftype( $subnode ) // ''; if ( ( $subtype eq 'ARRAY' ) and ( @$subnode == 0 ) ) { delete $node->[$i]; } else { prune3( $subnode ); } } } return ( $_[0] ); } sub prune4 { my ( $arref ) = @_; my @arr1 = @$arref; my @basket1; my $counter1 = 0; foreach my $ref1 ( @arr1 ) { my @elts1 = @$ref1; my @basket2; my $counter2 = 0; foreach my $ref2 ( @elts1 ) { my @elts2 = @$ref2; my @basket3; my $counter3 = 0; foreach my $ref3 ( @elts2 ) { my @elts3 = @$ref3; my @basket4; unless ( ( @elts3 == undef ) or ( @elts3 == () ) or ( +@{ $ref3->[$counter3] } ) ) { push ( @basket3, [ @basket4, $ref3 ] ); } $counter3++; } unless ( ( @elts2 == undef ) or ( @elts2 == () ) or ( @{ $ +ref2->[$counter2] } ) ) { push ( @basket2, [ @basket3, $ref2 ] ); } $counter2++; } unless ( ( @elts1 == undef ) or ( @elts1 == () ) or ( @{ $ref1 +->[$counter1] } ) ) { push ( @basket1, [ @basket2, $ref1 ] ); } $counter1++; } return ( \@basket1 ); } sub prune5 { # USING Array::Deeputils my $node = $_[0]; purge( $node, [] ); purge( $node, '[]' ); purge( $node, "" ); return ( $node ); } my @a = ( 1, 2, [], 4, [ 11, 22, [], 44 ] ); my $resultref = prune1( \@a ); my @result = @$resultref; #my $resultref = prune2( \@a ); my @result = @$resultref; #my $resultref = prune3( \@a ); my @result = @$resultref; #my $resultref = prune4( \@a ); my @result = @$resultref; #my $resultref = prune5( \@a ); my @result = @$resultref; say "result: " . Dumper( @result );

In reply to pruning of empty arrayrefs in nested array by building_arch

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.