in reply to Reporting empty cells in a table of records
Hi EchoAngel,
If you want to find which elements are missing from a list, you've got to know the complete set of elements. I'm sure there's a better, more perlish way to do this (using grep, Quantum::Superpositions, Set::Scalar, Set::Bag?), but here is how I would do it.
First, create a list of all the fruits. I don't quite understand how your AA-ZZ fits in, but it looks like 'pearsWE' eq 'pearsDF' so I'll assume they are not part of the name (I'm creating a new hash without those characters for simplicity). It also looks like all the fruit names are in lowercase, so I'll go on that assumption as well. Second, cycle through each country and check to see which elements are missing from the list of fruits.
my ( %fruits, %newHASH ); # find all the fruit names foreach my $country ( keys %HASH ) { foreach my $fruit ( keys %{ $HASH{$country} } ) { $fruit =~ m/([a-z]+)[A-Z]+/; # [A-Z]+ included for distinction $fruits{$1}++; $newHASH{$country}{$1} = $HASH{$country}{$fruit}; } } # find which fruits are missing foreach my $country ( keys %newHASH ) { foreach my $fruit ( keys %fruits ) { if( not exists $newHASH{$country}{$fruit} ) { print "$country is missing $fruit!\n"; } } }
BTW, if you don't need a hash for the fruits, a hash of arrays might be more appropriate than a hash of a hash. In other words, instead of using:
%HASH $country $fruit = 1 $fruit = 1
you might consider:
%HASH $country = [ $fruit, $fruit... ]
(which might make comparing lists easier).
HTH
|
|---|