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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |