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 );

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

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
    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.
    I certainly do not want to be nitpicking with you, but it seems to me that deleting an array element does not exactly sets it to undef, but rather to "empty slot". If you undef an array element, you obtain something like this:
    DB<14> x \@a 0 ARRAY(0x6005d1060) 0 0 1 1 2 undef 3 3 4 4
    whereas if you delete it (or if you don't define it in the first place), you get:
    DB<4> x \@array; 0 ARRAY(0x600500b60) 0 'O' 1 1 2 empty slot 3 3 4 4
    It does not make a practical difference for the programmer checking if the value is defined, both values will appear to be undefined, but it seems that, internally, Perl is not doing exactly the same thing. I have no idea of what the difference is.

    Je suis Charlie.
      I certainly do not want to be nitpicking with you, but it seems to me that deleting an array element does not exactly sets it to undef,
      @a = 1..3;; pp \@a;; [1, 2, 3] delete $a[1];; pp \@a;; [1, undef, 3]

      Close enough for me :)

      but rather to "empty slot".... I have no idea of what the difference is.

      A Perl array consists of a C array of pointers to scalars.

      delete sets the value of the C pointer to 0; and the debugger reports that as "Empty slot"; but in all other circumstances is detected and reported as undef.

      If you manually set the value of an array element to undef, then the C array pointer is set to point to the undef, which is a static pointer value that is defined to be undefined :)

      It does not make a practical difference for the programmer

      Agreed!


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
      In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked