Fellow Monasterians,
In trying to avoid the dreaded symbolic variable (building a variable from another as recently discussed), I have generated some code I know can be made simpler (this code *does* work). It does seem like the perfect place for the symbolic vs. the hash method--partly because I already have a mish-mash of hashes to prep my data for HTML::Template.
I apologize for the lengthy code. This is greatly simplied--there are actually 14 AoHs to build. How can I get this widdled down to a few loops? TIA!
$template = HTML::Template->new(filename => "../articlesummary.tmpl"); my $subtopic = $query->param('sub'); (11a, 11b, 22, 33) #really 14 +of these my $stmt = "SELECT id,title,goal FROM articles WHERE goal LIKE '$subto +pic.%'"; my $sth = execute_it($dbh,$stmt); #prepare and execute sub; my $sqldata = $sth->fetchall_arrayref({}); if ($sqldata) { my ($ctr1,$ctr2,$ctr3,$ctr4); my ($data11a, $data11b, $data22, $data33); for my $i ( 0 .. $#$sqldata ) { if ($sqldata->[$i]{'goal'} eq "11a") { $data11a->[$ctr1]{id} = $sqldata->[$i]{'id'}; $data11a->[$ctr1]{title} = $sqldata->[$i]{'title'}; $data11a->[$ctr1]{summary} = $sqldata->[$i]{'summary'}; $ctr1++; } if ($sqldata->[$i]{'goal'} eq "11b") { $data11b->[$ctr1]{id} = $sqldata->[$i]{'id'}; $data11b->[$ctr1]{title} = $sqldata->[$i]{'title'}; $data11b->[$ctr1]{summary} = $sqldata->[$i]{'summary'}; $ctr2++; } if ($sqldata->[$i]{'goal'} eq "22") { $data23->[$ctr1]{id} = $sqldata->[$i]{'id'}; $data23->[$ctr1]{title} = $sqldata->[$i]{'title'}; $data23->[$ctr1]{summary} = $sqldata->[$i]{'summary'}; $ctr3++; } if ($sqldata->[$i]{'goal'} eq "33") { $data33->[$ctr1]{id} = $sqldata->[$i]{'id'}; $data33->[$ctr1]{title} = $sqldata->[$i]{'title'}; $data33->[$ctr1]{summary} = $sqldata->[$i]{'summary'}; $ctr4++; } } if ($data11a) { $template ->param(list11a => $data11a); } if ($data11b) { $template ->param(list11b => $data11b); } if ($data22) { $template ->param(list22 => $data22); } if ($data33) { $template ->param(list33 => $data33); } }
Would like to use something like:
my goals qw(11a, 11b, 22, 33); for my $i ( 0 .. $#$sqldata ) { for my $j ( 0 .. $#goals ) { if ($sqldata->[$i]{'goal'} eq $goals[$j]) { $data$goals[$j]->[$ctr]{id} = $sqldata->[$i]{'id'}; $ctr++; } } }
HTML: (14 of these)
<tmpl_if list11b> <tmpl_loop list11b> <h4><tmpl_var title></h4> <p><tmpl_var summary></p> <p><a href="cgi-bin/show.pl?id=<tmpl_var id>">Read article...</a +></p> </tmpl_loop> </tmpl_if>
In reply to Bloated code of loops and conditionals by bradcathey
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |