If you you can fetch the data ordered (as your example suggests), you do not mind having extra keys in the episode hashrefs, and you know that your seasons run from first to n-th, you could fetch like this:
my $appearances = [];
while ( my $ref = $sth->fetchrow_hashref() ) {
my $season = $ref->{season};
unless ( exists $appearances->[$season -1 ] ) {
$appearances->[$season -1 ] = { season => $season, eps => [] };
}
my $eps = $appearances->[$season -1 ]->{eps};
push @$eps, $ref;
}
This should give you
$appearances =
[
{season => 1, eps =>
[
{season => 1, ep => 1, title => 'Hellmouth' },
......
Disclaimer: untested
Update: Note that since this code recycles the hashref from DBI, if you add more fields in your SQL later, they will show up in the final data structure as well.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.