biohisham has asked for the wisdom of the Perl Monks concerning the following question:
From the documentation for Text::Table, it doesn't seem that the module has capabilities that allow it to distinguish blank values from non-blank values when generating tables and thus discard them blanks (or squeeze them), I want to come up with a way of circumventing this and I thought skipping over a column value if it is blank can be an option since Text::Table handles data column-wise, but consulting the documentation for it and its dependency Text::Aligner hasn't indicated a trail.
What I have tried so far proved unsuccessful too, I have tried to check the defined status by if($element) where $element is an array member in a foreach loop, I also thought that I can interpolate a subroutine return value to the load function of Text::Table and that the subroutine would only return these values that are "defined" from the array elements passed to it but again no success, then I fiddled with if($element ne ''){} and variants thereof...
Presumably, such an approach can be possible but clearly is it beyond my humble Perl hacking capabilities ergo revert I to the wise Monastery monks for their guidance and take on this matter...
Here is just a generalization representing a simplification of the issue I am tackling but my original data is an advanced structure which can potentially be wrangled to fit the recommended approach
use strict; use warnings; use Text::Table; my $tb = Text::Table->new( "Planet\n---------" #header ); my @planets = qw('Uranus' '' 'Pluto'); printReport(@planets); sub printReport{ my @array = @_; foreach my $element(@array){ if(!$element){ next; } else{ #fill the body $tb->load([$element]); } } }; print $tb;
instead of the intendedUranus '' Pluto
Uranus Pluto
UPDATE: Suggestion by moritz tipped me to check the Text::Table source code, the module overloads '' and stringify it hence I suppose perl wouldn't but respond according to the new meaning of '', I require to be corrected. This observation didn't essentially solve my original issue, but it cut the running-in-circles and here's my walk-around approach to perform this task, flagging empty fields with a placeholder and skipping produces the required output:
use strict; use warnings; use Text::Table; my $tb = Text::Table->new( "Planet\n---------" #header ); my @planets = qw('Uranus' E 'Pluto'); # E for empty printReport(@planets); sub printReport{ my @array = @_; foreach my $element(@array){ if($element eq 'E'){ next; }else{ #fill the body $tb->load([$element]); } #$element eq 'E'? next : $tb->load([$element]); #$tb->load([$element]) if (defined $element and length +$element); } }; print $tb;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: purge Text::Table empty columns
by moritz (Cardinal) on May 18, 2010 at 12:23 UTC | |
|
Re: purge Text::Table empty columns
by thundergnat (Deacon) on May 18, 2010 at 13:08 UTC |