O Monks, hallowed be your woodiness.

For a variety of reasons, I'm collecting words alongside their date of encounter into a database. Then I group them by their woodiness or tinniness. The database table has three columns and looks like this:

30 Sep (Sun) | woody | pert 30 Sep (Sun) | tinny | newspaper 01 Oct (Mon) | woody | ocelot

Now, to create a more compact view, I'd like to transform the one-word-per-row raw data into an HTML table grouped by date like this:

date | woody words | tinny words -------------+--------------------------+------------- 28 Sep (Fri) caribou litterbin 29 Sep (Sat) wasp, yowling, gorn 30 Sep (Sun) intercourse, bound, pert newspaper 01 Oct (Mon) ocelot, concubine antelope 02 Oct (Tue) vole, sausage 03 Oct (Wed) recidivist, tit

However, I'm having quite a bit of trouble with the loop logic. I can manage the grouping to date, but the special cases of days where only one type of words appear don't quite work and either the words go into the wrong column or the column is left without (empty) markup.

(I actually created a nice generic function that was supposed to solve this sort of task, but it ended up lacking in a few ways. Its interface borders on the silly (expects 4+ subrefs.))

Attached is sample data along with an iterator that emulates a database query handle.

sub make_generator { my ($day, $type, $word) = @_; return sub { my $line = <DATA>; chomp $line; return unless $line; ($$day, $$type, $$word) = split(/\s+\|\s+/, $line); return 1; }; } my $gen = make_generator(\my ($day, $type, $word)); while ($gen->()) { print "$day: encountered '$word', a $type word\n"; } __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

In reply to Pivoting parts of a database table into an HTML table by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.