Thanks to everyone who responded. I learned a lot from the suggested code fragments, as well as from Data::Grouper. However, none of these were meeting my needs. Specifically, I wanted to specify more than one fields in both outer and inner loops. Anyway, I finally cracked it, and since I spent two days staring at the screen trying to figure it out, I offer the following with the hope that some other lost soul might find it timesaving and of use.

Assuming I have @res as below (returned from a $sth->fetchall_arrayref({}))

id,maker,make
===============
1,gm,saturn
1,gm,chevy
1,gm,caddy
2,ford,taurus
2,ford,escort
3,vw,bug
3,vw,jetta
3,vw,phaeton
as an array of hashrefs (each row is a hash). And I want a normalized array of hashrefs with nested loops (suitable for H::T). I wrote the following that works (please do suggest any improvements I might be able to make to this) --
my $groupby = 'id'; # field to group by my @outerfields = ('maker'); # outer loop fields my @innerfields = ('make'); # inner loop fields my $cars = &normalize(\@res, $groupby, \@outerfields, \@innerfields); $template->param(cars => $cars); # print it all print "Content-Type: text/html\n\n", $template->output; sub normalize($aref, $groupby, $outerfields, $innerfields) { my ($aref, $groupby, $outerfields, $innerfields) = @_; my @res = @$aref; my @outerfields = @$outerfields; my @innerfields = @$innerfields; my $seen = 0; # a flag to remember seen records my @temp; # a temp array to store outer loop fields my %makes; # a hash to store all the makes keyed by the grou +pby field # loop over the denormalized results foreach (@res) { # using the groupby field, test if the record has been seen... if ($_->{$groupby} != $seen) { # create a hash to store the outer loop fields, the car maker my %q; $q{$groupby} = $_->{$groupby}; foreach my $of (@outerfields) { $q{$of} = $_->{$of}; } # stuff the outer loop fields hash into the temp array push(@temp, \%q); # create a hash to store the inner loop fields, the car makes my %make; foreach my $if (@innerfields) { $make{$if} = $_->{$if}; } # add the inner loop fields hash to the makes hash keyed by the +groupby field push(@{$makes{$_->{$groupby}}}, \%make); # update the flag $seen = $_->{$groupby}; # since the record has already been seen... } else { # create a hash to store the inner loop fields, the car makes my %make; foreach my $if (@innerfields) { $make{$if} = $_->{$if}; } # add the inner loop fields hash to the makes hash keyed by the +groupby field push(@{$makes{$_->{$groupby}}}, \%make); } } my @cars; # an array to hold it all in its final form # loop over the temp array created above foreach (@temp) { # grab the makes for this maker using the groupby field and # add it to this row as an array of hashrefs $_->{'makes'} = $makes{$_->{$groupby}}; # add the entire row, a hashref, to the final array push(@cars, $_); } return \@cars; # return back to the code }
Meanwhile, in my html template
<tmpl_loop cars> <tmpl_var id>. <tmpl_var maker> <tmpl_loop makes> <tmpl_var make> </tmpl_loop> </tmpl_loop>

In reply to Re: normalizing results from a db query by punkish
in thread normalizing results from a db query by punkish

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.