sub printHash { my ( $hash_name, $hash_ref ) = @_; open( LIST, ">", $hash_name ) or die $!; print LIST sort map { "$_ $$hash_ref{$_}\n" } keys %$hash_ref; close LIST; } ... # assume you have an "ur-hash", holding hash_refs keyed by hash_name: my @difflist1 = my @difflist2 = sort keys %all_hashes; foreach my $name ( keys %all_hashes ) { printHash( $name, $all_hashes{$name} ); } my %distances; foreach my $h1 ( @difflist1 ) { shift @difflist2; # dispose of the identical item in list2 foreach my $h2 ( @difflist2 ) { $distances{"$h1:$h2"} = `diff $h1 $h2 | grep -c '^<>'`; } } # then do something with the values in %distances