OK I know this code is hideous, believe me. That's why I'm seeking the wisdom! It's more of a logic problem than simply a Perl problem, but...

I'm getting some data out of SQL (fetchrow_hashref) in the form:

{season => 1, ep => 1, title => 'Hellmouth' }, {season => 1, ep => 2, title => 'Harvest' }, {season => 1, ep => 3, title => 'Witch' }, {season => 1, ep => 4, title => 'Teacher' }, {season => 1, ep => 5, title => 'First Date'}, # [etc] {season => 2, ep => 13, title => 'Bad' }, {season => 2, ep => 14, title => 'Assembly' }, {season => 2, ep => 15, title => 'School' }

That is, it's a long list of episodes, some in season n, some in season n+1 and so on. I need to put it into a structure like this:

$appearances = [ {season => 1, eps => [ {title => 'Hellmouth' }, {title => 'Harvest' }, {title => 'Witch' }, {title => 'Teacher' }, {title => 'First Date'} ] }, {season => 2, eps => [ {title => 'Bad' }, {title => 'Assembly' }, {title => 'School' }, ] } ]

An array of hashes for each season, and each episode an entry in an array within that hash.

Here's how I did it late last night, and it works, but there must be a better way than this:

my $temp_hash = {}; my $last_season = 0; my $appearances = []; while ( my $ref = $sth->fetchrow_hashref() ) { if ( $ref->{season} != $last_season ) { # if the season has changed, unless unless ( $last_season == 0 ) { # unless it's the first season we encounter push( @{$appearances}, $temp_hash ); # put the data for that season into # the data structure $temp_hash = {}; # make the hash empty again } } $temp_hash->{'season'} = $ref->{'season'}; push( @{ $temp_hash->{'eps'} }, { title => $ref->{'title'} } ); $last_season = $ref->{season}; # put the season into the marker variable } push( @{$appearances}, $temp_hash ); # need to put what's left over into the data # structure after the while() finishes.

Where I build little structures and append them onto the big structure by watching for a change of season, with extra clauses for the first time around and so on.

I await your advice. Please be kind.

Update: Forgot to say, yes the data comes out in the right order, that's not part of the problem, and no, it's not an HoAoH, is it? It's an AoHoWhatever.



($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
=~y~b-v~a-z~s; print

In reply to building an HoAoH ... very badly by Cody Pendant

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.