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. | [reply] [d/l] |
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. | [reply] [d/l] |