OldMonkRum has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks,
I am using
push(@array,['X1','X2','X3','X4' ]);
Subsequently I am doing:
push(@array,['',$key,'','']);
Because X2 elements are coming from a certain hash and so on.
push(@array,[$key,'','','']);
Then I am using HTML::QuickTable
When I render: $qt->render(\@array);
The table has first column empty values till it hits an actual value. Any way, I can print only when its not empty i.e. not waste rows in the table?
Thanks a lot!
Re: Removing empty elements from multi-dimensional array
by hippo (Bishop) on Jul 18, 2017 at 12:48 UTC
|
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
my @array = (
['X1','X2','X3','X4' ],
['','foo','',''],
['bar','','','']
);
my @nofirstempty = grep { length $_->[0] } @array;
print Dumper \@nofirstempty;
| [reply] [d/l] |
Re: Removing empty elements from multi-dimensional array
by Laurent_R (Canon) on Jul 18, 2017 at 12:51 UTC
|
I guess this is more or less what you're looking for (demonstrated under the Perl debugger):
DB<14> @array = (['X1','X2','X3','X4' ], ,['','key', ''], ['key', ''
+, '']);
DB<15> x \@array
0 ARRAY(0x600501580)
0 ARRAY(0x6005d2818)
0 'X1'
1 'X2'
2 'X3'
3 'X4'
1 ARRAY(0x6005015f8)
0 ''
1 'key'
2 ''
2 ARRAY(0x6004fa8c0)
0 'key'
1 ''
2 ''
DB<16> for my $subarrayref ( @array ) { $subarrayref = [ grep leng
+th $_, @$subarrayref] }
DB<17> x \@array
0 ARRAY(0x600501580)
0 ARRAY(0x6005d2d70)
0 'X1'
1 'X2'
2 'X3'
3 'X4'
1 ARRAY(0x600500e60)
0 'key'
2 ARRAY(0x600635c28)
0 'key'
| [reply] [d/l] |
Re: Removing empty elements from multi-dimensional array
by OldMonkRum (Novice) on Jul 18, 2017 at 12:59 UTC
|
#!/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
| [reply] [d/l] |
|
#!/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. | [reply] [d/l] |
|
#!/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
| [reply] [d/l] |
Re: Removing empty elements from multi-dimensional array
by 1nickt (Canon) on Jul 18, 2017 at 13:05 UTC
|
Sounds more like an issue with the output engine than with your data structure. For an alternative, you might like to check out Spreadsheet::HTML by jeffa.
The way forward always starts with a minimal test.
| [reply] |
|
|