Fellow Monasterians,

Spring is in the air, so time to dust off the golf clubs, or at least the refactoring tools.

I'm prepping an AoH that I'm pulling out of a database for use in a HTML::Template nested <tmpl_loop>. I'm getting the desired result, but my method (the for loop) seems a bit kludgy. So, is there anything that I can do to make this more orthodox or Perlish?

HTML in a .tmpl

<tmpl_loop case_list> <tmpl_var examiner> <tmpl_loop casedata> <tmpl_var examiner> <tmpl_var case> <tmpl_var location> </tmpl_loop> </tmpl_loop>

Perl

my $all_cases = [ #an AoH from a DBI query { 'examiner' => 'Brad', 'case' => '2345432', 'location' => 'Sandwich' }, { 'examiner' => 'Brad', 'case' => '7678979', 'location' => 'Wheaton' }, { 'examiner' => 'Mary', 'case' => '1234534', 'location' => 'Geneva' }, { 'examiner' => 'Mary', 'case' => '8966789', 'location' => 'Aurora' }, ]; my $case_list; my $temp = ""; my $cctr = -1; for my $i ( 0 .. $#$all_cases ) { if ( $all_cases->[$i]{'examiner'} ne $temp ) { $cctr++; $temp = $all_cases->[$i]{'examiner'}; $case_list->[$cctr]{'examiner'} = $temp; } push @{ $case_list->[$cctr]{'casedata'} }, $all_cases->[$i]; } $template->param( case_list => $case_list ); __OUTPUT__ [ { 'examiner' => 'Brad', 'casedata' => [ {'case' => '2345432', 'examiner' => 'Brad', 'location' => 'Sandwich'}, {'case' => '7678979', 'examiner' => 'Brad', 'location' => 'Wheaton'} ], } { 'examiner' => 'Mary', 'casedata' => [ {'case' => '1234534', 'examiner' => 'Mary', 'location' => 'Geneva'}, {'case' => '8966789', 'examiner' => 'Mary', 'location' => 'Aurora'} ], } ];

Thanks in advance.

—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot

In reply to Cleaner build of an AoH for nested loop? by bradcathey

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.