in reply to Pivoting parts of a database table into an HTML table
Here is a script which collates the data and produces the desired compact view:
#! perl use strict; use warnings; use feature 'switch'; { my %h; populate (\%h); stringify (\%h); print_view(\%h); } sub populate { my ($h) = @_; while (<DATA>) { my ($date, $type, $word) = split /\s+\|\s+/; chomp $word; unless (exists $h->{$date}) { $h->{$date}{woody_words} = []; $h->{$date}{tinny_words} = []; } given ($type) { when ('woody') { push @{ $h->{$date}{woody_words} }, $word +; } when ('tinny') { push @{ $h->{$date}{tinny_words} }, $word +; } default { warn "Datum '$word' of unknown type '$typ +e'"; } } } } sub stringify { my ($h) = @_; for (keys %$h) { $h->{$_}{woody_str} = join(', ', sort @{ $h->{$_}{woody_words} + }); $h->{$_}{tinny_str} = join(', ', sort @{ $h->{$_}{tinny_words} + }); } } sub print_view { my ($h) = @_; my $max = 0; for (keys %$h) { my $woody_str = $h->{$_}{woody_str}; my $new_length = length $woody_str; $max = $new_length if defined $woody_str && $new_length > $max +; } printf " date | %-*s | tinny words\n", $max, 'woody words'; printf "-------------+-%s-+-------------\n", '-' x $max; printf "%s %-*s %s\n", $_, $max, $h->{$_}{woody_str}, $h->{$_}{ +tinny_str} for sort { cmp_dates() } keys %$h; } { my %months; BEGIN { %months = (Jan => 1, Feb => 2, Mar => 3, Apr => 4, May => 5, Jun => 6, Jul => 7, Aug => 8, Sep => 9, Oct => 10, Nov => 11, Dec => 12); } sub cmp_dates { my ($day_a, $mon_a) = $a =~ /^(\d{1,2}) (\w{3})/; my ($day_b, $mon_b) = $b =~ /^(\d{1,2}) (\w{3})/; return ($months{$mon_a} < $months{$mon_b}) ? -1 : ($months{$mon_a} > $months{$mon_b}) ? +1 : ($day_a < $day_b) ? -1 : ($day_a > $day_b) ? +1 : 0; } } __DATA__ 28 Sep (Fri) | woody | caribou 28 Sep (Fri) | tinny | litterbin 29 Sep (Sat) | woody | wasp 29 Sep (Sat) | woody | yowling 29 Sep (Sat) | woody | gorn 30 Sep (Sun) | woody | intercourse 30 Sep (Sun) | woody | bound 30 Sep (Sun) | woody | pert 30 Sep (Sun) | tinny | newspaper 01 Oct (Mon) | woody | ocelot 01 Oct (Mon) | woody | concubine 01 Oct (Mon) | tinny | antelope 02 Oct (Tue) | woody | vole 02 Oct (Tue) | woody | sausage 03 Oct (Wed) | tinny | recidivist 03 Oct (Wed) | tinny | tit
Output:
23:09 >perl 345_SoPW.pl date | woody words | tinny words -------------+--------------------------+------------- 28 Sep (Fri) caribou litterbin 29 Sep (Sat) gorn, wasp, yowling 30 Sep (Sun) bound, intercourse, pert newspaper 01 Oct (Mon) concubine, ocelot antelope 02 Oct (Tue) sausage, vole 03 Oct (Wed) recidivist, tit 23:10 >
Of course, the really interesting question is: How do you distinguish words which are ‘woody’ from those which are ‘tinny’? ;-)
Hope this helps,
Updates: Minor code improvements.
Athanasius <°(((>< contra mundum
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Pivoting parts of a database table into an HTML table
by Anonymous Monk on Oct 22, 2012 at 15:16 UTC | |
by jandrew (Chaplain) on Oct 22, 2012 at 17:45 UTC | |
by Anonymous Monk on Oct 23, 2012 at 07:20 UTC | |
by jandrew (Chaplain) on Oct 24, 2012 at 17:37 UTC |