Here's one way. I am assuming that within a given season the episodes are returned in ascending order. Since this is from a database query, if that isn't true now, just modify the SQL to make it true. First I build a HoAoH indexed by season number.
my %temp_hash; while (my $ref = $sth->fetchrow_hashref) { my $season = $ref->{season}; my $title = $ref->{title}; push @{$temp_hash{$season}}, { title => $title }; }
Then I build it out.
my $appearances = [ map { +{ season => $_ , eps => $temp_hash{$_} } } sort keys %temp_hash ];

Update: This solution has one small advantage in that it could work even if the seasons weren't numeric, but I kind of like broquaint's because it skips the intermediate step and also takes advantage of the seasons being ordered, which mine doesn't.


In reply to Re: building an HoAoH ... very badly by Errto
in thread 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.