in reply to normalizing results from a db query

I don't think I understand your problem. In your else block you won't have access to the foo declared in the if (cond1) statement because the code in the if block has never been executed.

Have you declared foo outside the while loop? Or are you looking to do something with an empty array foo? If the latter, try this:
while (looping over something) { my @foo; 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 } }

Replies are listed 'Best First'.
Re: Re: normalizing results from a db query
by biosysadmin (Deacon) on Jan 26, 2004 at 06:45 UTC
    I have a better understanding of your problem now, here's the code for my solution to problem #2. It's a bit hackish, but I thought I'd throw it out as one way to do it:
    #!/usr/bin/perl -w use strict; use Data::Dumper; my $seen = 0; my @carmakers; while (<DATA>) { chop; my ($id, $maker, $make) = split(",", $_); if ($id != $seen) { package block1; my %maker; $maker{'thismaker'} = $maker; my @makes; my %make = ('thismake' => $make); push(@makes, \%make); $maker{'makes'} = \@makes; push(@carmakers, \%maker); # problem1 $seen = $id; } else { package block2; my %make = ('thismake' => $make); push(@block1::makes, \%make); # problem2 $block1::maker{'makes'} = \@block1::makes; } } print Dumper( @carmakers ); __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
    I'll think some more about problem 1, this is fun. :) Have you thought about a different way of structuring your database quer(y|ies)? For example, maybe you could use the database to get a unique set of carmakers.
Re: Re: normalizing results from a db query
by punkish (Priest) on Jan 25, 2004 at 21:56 UTC
    well, that is precisely the problem. Here are more annotations --
    while (looping over something) { if (cond1) { create @foo if cond1 is met do a bunch of things, and go to the next iteration in the loop } else { since cond1 is not met, don't create a new @foo but do something to @foo already declared in the prev iteration do other things, and go to the next iteration in the loop } }
    This makes sense if you look at the context of my question. I am looping over a result set that has duplicates (carmakers with their makes). I want to eliminate the display of duplicate carmakers, but remember them in successive iterations so I can associate all their makes correctly to them.