#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=){ #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