In that case, I think you need two data structures. Here's some half-baked code.
while ( defined( my $record = <$input_fh> ) ) {
my %record_hash = rec2hash( $record );
while ( my ( $field, $value ) = each %record_hash ) {
my $variant = variant_of( $value );
$variants_in{ $record }{ $field }{ $variant } = 1;
push @{ $records_for{ $field }{ $variant } }, $record;
}
}
Here I store for each field-variant pair, every record that has that pair. Also for each record, I have every variant that's present in it. I leave it to you to take a record and turn it into field-value pairs, and I also leave it to you to turn a particular value into a "variant" (each value may be. a variant, but I can't tell from your description).
From here you figure out which field-variant pair you want to represent most by looping through the pairs in %records_for and finding which pair has the fewest records available. From that you get a list of records which exemplify that field-variant pair.
Then look in %variants_in for each record and see which one represents the most different field-variant pairs. That will be one of your examples.
Then you can delete that record from %variants_in, and you'll want to take every field-variant pair in that record and delete them from %records_for since you no longer need examples of them.
Then go back and pick another record of interest until there aren't any more.
Instead of a multi-level hash, it may make it easier to pick some separator and store field-variant pairs that way. That is, you do $records_for{ "$field$separator$variant" } instead of $records_for{ $field }{ $variant }. The problem with this is you have to make sure the separator never appears in your fields or variants.
Hope this helps.
|