I asked this on HTML::Template list, but am asking here as well because (well, no reply from H::T list yet, and I am in a hurry, ;-) , but also because of an additional general question). I am drawing a blank on how to accomplish the following (simplified example provided below) --

from a database query involving joins, I have the following array (actually an array of hashes)

1,gm,saturn
1,gm,chevy
1,gm,caddy
2,ford,taurus
2,ford,escort
3,vw,bug
3,vw,jetta
3,vw,phaeton
..
In order to display
1. gm
	- saturn
	- chevy
	- caddy
2. ford
	- taurus
	- escort
..

I can do the following code

#!/usr/bin/perl -w use strict; my $seen = 0; while (<DATA>) { chop; my ($id, $maker, $make) = split(",", $_); if ($id != $seen) { print "$id. $maker\n"; print " - $make\n"; $seen = $id; } else { print " - $make\n"; } } __DATA__ 1,gm,saturn 1,gm,chevy 1,gm,caddy 2,ford,taurus 2,ford,escort 3,vw,bug 3,vw,jetta 3,vw,phaeton

But I need to use this with H::T.
So, I really need an array of hashrefs. I really need something like --

my @carmakers = ( \%thismaker, \%thismaker, \%thismaker );
where
my %thismaker = ('thismaker' => 'gm', 'makes' => \@makes);
where
my @makes = (\%thismake, \%thismake, \%thismake);
where
my %thismake = ('thismake' => 'saturn'); my %thismake = ('thismake' => 'chevy'); my %thismake = ('thismake' => 'caddy');
in turn for each maker and within each maker for each make.

This will allow me to display the following --

<tmpl_loop carmakers> <tmpl_var thismaker> <tmpl_loop makes> <tmpl_var thismake> </tmpl_loop> </tmpl_loop>
Applying my logic as above doesn't work
my $seen = 0; my @carmakers; while (<DATA>) { chop; my ($id, $maker, $make) = split(",", $_); if ($id != $seen) { my %maker; $maker{'thismaker'} = $maker; my @makes; my %make = ('thismake' => $make); push(@makes, \%make); $maker{'makes'} = \@makes; push(@carmakers, \%maker); # problem1 $seen = $id; } else { my %make = ('thismake' => $make); push(@makes, \%make); # problem2 $maker{'makes'} = \@makes; } }
But, this is where I run into my logic problem. The line marked "# problem1" should not occur unless a new car maker is encountered. And the line marked "# problem2" fails because it does not see the @makes declared in the same block but in the first conditional.

Which brings me to a more general problem. Under "use strict" how do I access a variable declared in a block that is at the same level as the current block? In other words, if I have

while (looping over something) { if (cond1) { create @foo if a certain condition is met do a bunch of things } else { do something to foo already declared do a bunch of other things } }

janitored by ybiC: Increased scope of existing <readmore> tags


In reply to normalizing results from a db query by punkish

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.