bradcathey has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Cleaner build of an AoH for nested loop?
by kyle (Abbot) on Mar 06, 2009 at 04:49 UTC | |
by bradcathey (Prior) on Mar 06, 2009 at 13:53 UTC | |
by kyle (Abbot) on Mar 06, 2009 at 17:44 UTC | |
|
Re: Cleaner build of an AoH for nested loop?
by ig (Vicar) on Mar 06, 2009 at 05:57 UTC |