in reply to Removing empty elements from multi-dimensional array

#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; my @array = ( ['X1','X2','X3','X4' ], ['','foo','',''], ['','bar','',''], ['',rat','',''], ['me','','',''] ); ##Some processing on @array @array=??? my $qt = HTML::QuickTable->new(); $qt->render(\@array); Output should be: X1 X2 X3 X4 me foo bar rat Currently it is: X1 X2 X3 X4 foo bar rat me

Replies are listed 'Best First'.
Re^2: Removing empty elements from multi-dimensional array
by hippo (Archbishop) on Jul 18, 2017 at 13:42 UTC

    That's a bit more involved but Array::Transpose softens the blow a bit.

    #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; use Array::Transpose; my @array = ( ['X1','X2','X3','X4' ], ['','foo','',''], ['','bar','',''], ['','rat','',''], ['me','','',''] ); my @at = transpose (\@array); my @condensed; for my $row (@at) { my @shortrow = sort { (length ($b) > 0 <=> length ($a) > 0) } @$r +ow; push @condensed, \@shortrow; } @array = transpose (\@condensed); print Dumper \@array;

    Removal of trailing rows just containing blanks is left as an exercise to the reader.

Re^2: Removing empty elements from multi-dimensional array
by tybalt89 (Monsignor) on Jul 18, 2017 at 19:31 UTC
    #!/usr/bin/perl # http://perlmonks.org/?node_id=1195332 use strict; use warnings; my @array = ( ['X1','X2','X3','X4' ], ['','foo','',''], ['','bar','',''], ['','rat','',''], ['me','','',''] ); use Data::Dump 'pp'; print "before\n"; pp \@array; my @newarray; my @itemsincolumn; my $width = @{ $array[0] }; for my $row ( @array ) { for my $column ( 0 .. $width - 1 ) { length $row->[$column] and $newarray[ $itemsincolumn[$column]++ ][$column] = $row->[$column +]; } } for my $row ( @newarray ) { $_ .= '' for $row->@[0 .. $width - 1]; } print "after\n"; pp \@newarray