Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a large, sorted, CSV file arranged along these lines:

Desk,Left-Drawer,Paperclips Desk,Left-Drawer,Pens Desk,Right-Drawer,Ruler Filing-Cabinet,Top-Drawer,Files

To make it more human-readable, I would like to pretty-print it so that column entries are omitted if they duplicate the one above. The desired output would be something like this:

Desk | Left-Drawer | Paperclips | | Pens | Right-Drawer | Ruler Filing-Cabinet | Top-Drawer | Files

One way of doing this would be to build the table line-by-line, testing if $column_one_value == $last_column_one_value and blanking it if necessary. However, as the number of columns increases, this becomes increasingly ugly.

I was therefore wondering if there were any modules which would help? I’ve been looking through Text::ASCIITable, Text::Table and the like and several of the List ones, but without success. Does anyone know of a nicer way of doing this than looping through and testing for previous values?

Many thanks!

Replies are listed 'Best First'.
Re: Pretty-Print Table (Omitting Duplicates)
by Corion (Patriarch) on Apr 24, 2011 at 09:40 UTC

    I don't see how remembering the last line becomes "increasingly ugly":

    use strict; my @lines = map { [split /,/] } split /\s+/, <<CSV; Desk,Left-Drawer,Paperclips Desk,Left-Drawer,Pens Desk,Right-Drawer,Ruler Filing-Cabinet,Top-Drawer,Files CSV my @last_line; for my $line (@lines) { my $i = 0; $i++ while $line->[ $i ] eq $last_line[ $i ]; # Now, empty out the elements we don't want to output again # and copy over the rest of @$line my @output = (('') x $i, @{$line}[$i..$#$line]); # Output through table module of your choice print join "|", map {sprintf "%-20s",$_} @output; print "\n"; @last_line = @$line; };

    The uglyness stays the same no matter what your column count is.

      Thanks. Rather naïvely I was thinking I would have to hard-code each column’s comparison in which is why I thought it would become ugly after a while.

Re: Pretty-Print Table (Omitting Duplicates)
by chrestomanci (Priest) on Apr 24, 2011 at 20:23 UTC

    I was doing something similar a few years back. The solution I adopted was first to generate the table in memory as a simple 2 dimensional array of hashes, Then once the data had been turned into a table, I had another function that compared and merged neighbouring identical cells. Finally the table was rendered in it's final form. I was rendering to HTML, but I don't think it would be any harder to render to plain text, so Text::Table looks suitable.