This problem needs something more than an AoA. Whenever you have a problem needing one of anything, it is best to start thinking "HASH". In this case we will need three hashes. While reading in data we will use two: one to keep track of the array associated with each list name ("mylist_1", "mylist_2", etc) and one to keep track of the name of the longest list found so far for each list member. Once we have the two hashes, we still have another step: finding only the list names that show up as longest lists. That will require using values and map as well as yet another hash.
The solution to this problem is a good illustration of three distinct, but very important uses of hashes:
- as an association between a name and a bunch of data (a string, a list, another hash, an object, etc)
- as a way of keeping track of the "most-est" - the longest, the smallest, the best, the worst, etc.
- as a way of generating unique lists from a list of possibly duplicate values
Here is the code. Please make sure you understand it. Feel free to post a reply if you have any questions: understanding how this works is far more important than the solution.
#always good to start your scripts with these two lines
#they let the compiler do about half of the bug catching
#and can save you lots of work
use strict;
use warnings;
#First we find which is the longest list for each list
#member
my %hLists; #associate list name w/ list members
my %hLongest; #keep track of longest list for each mbr.
while(my $line=<DATA>){
#read in the list and its name
my @aListMembers=split(/\s+/,$line);
next unless scalar(@aListMembers); #skip empty lines
#remember the name of the list
my $sList = shift @aListMembers;
$hLists{$sList} = \@aListMembers;
# now for each member of the list record the name of
# the longest list found so far
my $iCount = scalar(@aListMembers);
foreach (@aListMembers) {
#we only care if its longer than what we already have
if (exists($hLongest{$_})) {
my $kLists = $hLongest{$_};
my $aList = $hLists{$kLists};
next if ($iCount <= scalar(@$aList));
}
$hLongest{$_} = $sList;
}
}
#now we only care about lists that show up as the
#longest list for at least one element so lets get
#a unique "list" of those. We do that by creating a hash
#whose keys are the thing we want to be unique. Then
#when we want the "unique list" we extract the keys of
#the hash.
my %hListsWeCareAbout = map { $_ => 1 } values %hLongest;
#now lets print out the results:
foreach (sort keys %hListsWeCareAbout) {
my $aList = $hLists{$_};
print "$_: @$aList\n";
}
__DATA__
mylist_1 sublist_153 sublist_87 sublist_876 sublist_78
mylist_6 sublist_8
mylist_2 sublist_12 sublist_34 sublist_09
mylist_3 sublist_87 sublist_09
mylist_7 sublist_8 sublist_9
mylist_9 sublist_56
Best, beth
Update: clarification of terminology, cleaned up some misuses of the word "list". Added sort to final print out. Added reminder about understanding the code. Added comments about how this problem illustrates 3 important uses of hashes.