(a,b) means "a,b" are one set. (e,f) and (f,g) means "e,f,g" are one set.

I would like to print them per "set", and I would like to print "set which has more items" comes first.
I mean like this.

e,g,f
a,b
c
d
h
i
j
k
l
...
z

my code below seems to me not smart. And in fact, I sorted the results with another script... I sorted results by their length...
It was o.k. at that time, but I think monks have more better ways and I would like to hear your suggestions.

use strict; use warnings; use Data::Dumper;
my @key_array = 'a' .. 'z';
my @set_array = ( ['a', 'b'], ['e','f'], ['f','g'] );
#return it's id if they already exists in $set_hash
#%set_hash should be like { a=>1, b=>1, e=>2, f=>2, g=>2}
sub search_id {
my( $set_hash, $k1,$k2)=@_;
if ( exists( $set_hash->{$k1} ) ){
return $set_hash->{$k1};
} elsif ( exists( $set_hash->{$k2} ) ){
return $set_hash->{$k2};
} else {
return 0;
}
}
my (%key_hash, %set_hash, %set_hash_reverse, $id);
$key_hash{$_}=0 for @key_array; #0 for initial, increment if they were
+ sets which has more than one item
$id=1;
for ( @set_array ){
my ($k1,$k2) = ($_->[0], $_->[1]);
if ( exists($key_hash{$k1}) && exists($key_hash{$k2}) ){
if ( my $searched_id = search_id( \%set_hash, $k1,$k2) ){
$set_hash{ $k1 } = $searched_id;
$set_hash{ $k2 } = $searched_id;
} else {
$set_hash{ $k1 } = $id;
$set_hash{ $k2 } = $id;
$id++;
}
#increment
$key_hash{$k1}++;
$key_hash{$k2}++;
} else {
#no set or both set. die for error;
die "there is $k1 but no $k2."
if ( exists($key_hash{$k1}) && (!exists($key_hash{$k2})) )
+;
die "there is $k2 but no $k1."
if ( exists($key_hash{$k2}) && (!exists($key_hash{$k1})) )
+;
}
}
#{a=>1,b=>1, e=>2,f=>2,g=>2} to {1 =>[a,b],2=>[e,f,g]}
for (keys %set_hash){
push @{ $set_hash_reverse{$set_hash{$_}} }, $_;
}
#print out
for (sort {$b <=> $a} keys %set_hash_reverse){
print join(',', @{$set_hash_reverse{$_}} ), "\n";
}
for ( grep {$key_hash{$_} == 0 } sort {$a cmp $b} keys %key_hash ){
print "$_\n";
}