Athanasius's solution is the traditional perl solution for the problem you presented. In other words put the data in a hash (to organize the pivot) and then post process the output.

If you are trying to leverage some of the databases functionality as you indicated in your second post then you may want to consider $hash_ref = $sth->fetchall_hashref(); rather than the database sort.

If the goal is to use chapter 4 of Dominus's book to minimize memory overhead then you really just have a recursive problem where you need to identify your base state. (a change of date) You might find the following useful in that case. The data must be pre-sorted by date for this to work. Width formatting is not possible in this output since the data will be printed prior to a test of all rows. That shouldn't be a problem for your suggested goal of web output.

#! perl use Modern::Perl; my $data; #load the data my $last_date ; while (<DATA>){ my ($date, $type, $word) = split(/\s+\|\s+/, $_); chomp $word; # Check for a new date - base state if( $last_date and $date ne $last_date ){ print_out( $last_date, $data ); $data = {}; } if( exists $data->{$type} ){ $data->{$type} .= ', ' . $word; }else{ $data->{$type} = $word; } $last_date = $date; } # Pick up the last line print_out( $last_date, $data ); sub print_out{ my ( $date, $data ) = @_; print $date . ' | ' . ($data->{woody} // '') . ' | ' . ($data->{tinny} // '') . "\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

Produces this output

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

TIMTOWTDI!

Update1: changed for to while to honor the iterator concept

Update2: I just noticed I didn't pick up the last line (no base state test when the query ends. (fixed))

Update3: fetchrow_hashref is a better fit and can be used with sort


In reply to Re^3: Pivoting parts of a database table into an HTML table by jandrew
in thread 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.