I have a flat result set from a database, joining across three tables, for the sake of this example, call them magazines, issues and articles. Each magazine has a name, each issue a number and each article a name. The result set comes back something like:
$result = [ { mag_name => "mag1", issue_num => "01", article => "art1" }, { mag_name => "mag1", issue_num => "01", article=> "art2" }, { mag_name => "mag1", issue_num => "02", article=> "art1" }, { mag_name => "mag1", issue_num => "02", article=> "art2" }, { mag_name => "mag3", issue_num => "01", article=> "art1" } ] ;
Straightforward stuff so far. Now, that structure needs to be mangled into something totally different for the benefit of HTML::Template whilst maintaining the order of things:
$data = [ { 'magazine_name' => 'mag1', 'issue_loop' => [ { issue_number => '01', article_loop => [ { article_name => 'art1' } { article_name => 'art2' } ], }, { issue_number => '02', article_loop => [ { article_name => 'art1' } { article_name => 'art2' } ] }, { 'mag_name' => 'mag2', 'issue_loop' => [ { issue_number => '01', article_loop => [ { article_name => 'art1' }, { article_name => 'art2' } ] }, { issue_number => '02', article_loop => [ { article_name => 'art1' }, { article_name => 'art2' } ] } ] ;
I've probably mislaid some parens there but hey.

So what's a nice way of transmogrifying the former into the latter whilst preserving the order of things?

TIA,

In reply to Idiomatic way of getting from database result set to HTML::Template data structure? by davehodg

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.