in reply to Looking for Perl Elegance!
#! /bin/perl ## what we'll name our columns -- note that there is no ## provision for having more data items than columns. They ## will simply get displayed formatted corretly but ## w/o column headings my @cols = qw/TITLE DATA-A DATA-B DATA-C/; ## if we assume your data is arranged like so: ## (which also assumes no title or data can contain ## a '#' char) my @data = ( 'TITLE#How are you#item1#item2#item3', 'item1a#item2b#item3c', 'TITLE#I am fine#item4#item5#item6', ); my $fixed_width = 16; # assume fixed-width for table cells ## If you wanted to you could use the substr() function to ## enforce the column width print join(' ', map{ sprintf("%-*s", $fixed_width, $_) } @cols), "\n" +; ## Then this is an easy way to pick it apart and display ## it as a table: foreach (@data) { my ($title, @data) = map{ /#/? split(/#/, $_) : $_ } (/^\s*(?:TITL +E#(.*?)#)?(.*?)$/g); print join(' ', map{ sprintf("%-*s", $fixed_width, $_) } ($title, + @data)), "\n"; }
|
---|