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>

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

In reply to Bloated code of loops and conditionals 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.