punkish has asked for the wisdom of the Perl Monks concerning the following question:
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
wheremy @carmakers = ( \%thismaker, \%thismaker, \%thismaker );
wheremy %thismaker = ('thismaker' => 'gm', 'makes' => \@makes);
wheremy @makes = (\%thismake, \%thismake, \%thismake);
in turn for each maker and within each maker for each make.my %thismake = ('thismake' => 'saturn'); my %thismake = ('thismake' => 'chevy'); my %thismake = ('thismake' => 'caddy');
This will allow me to display the following --
Applying my logic as above doesn't work<tmpl_loop carmakers> <tmpl_var thismaker> <tmpl_loop makes> <tmpl_var thismake> </tmpl_loop> </tmpl_loop>
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.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; } }
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: normalizing results from a db query
by jdtoronto (Prior) on Jan 25, 2004 at 22:16 UTC | |
by punkish (Priest) on Jan 25, 2004 at 23:48 UTC | |
|
Re: normalizing results from a db query
by biosysadmin (Deacon) on Jan 25, 2004 at 21:48 UTC | |
by biosysadmin (Deacon) on Jan 26, 2004 at 06:45 UTC | |
by punkish (Priest) on Jan 25, 2004 at 21:56 UTC | |
|
Re: normalizing results from a db query
by Ctrl-z (Friar) on Jan 25, 2004 at 23:46 UTC | |
|
Re: normalizing results from a db query
by exussum0 (Vicar) on Jan 26, 2004 at 02:48 UTC | |
|
Re: normalizing results from a db query
by punkish (Priest) on Jan 26, 2004 at 06:05 UTC |