#!/usr/bin/perl -w use strict; my $candidate = "the the the the the the the"; my $reference = "the cat is the on the mat"; my %cand_histogram; my %ref_histogram; $cand_histogram{$_}++ foreach (split(/\s+/,$candidate)); $ref_histogram{$_}++ foreach (split(/\s+/,$reference)); my %seen; printf "%-6s %-10s %-10s\n", 'Key','Candidate','Reference'; foreach my $key ( sort { $seen{$b} <=> $seen{$a} #descending word cnt or $a cmp $b #alphabetic otherwise } grep {!$seen{$_}++} # each key just once, # but count 'em also!, # 2 means => in both hashes (keys %cand_histogram, keys %ref_histogram) ) { printf "%-6s %-10s %-10s\n", $key, $cand_histogram{$key}||='0', $ref_histogram{$key} ||='0'; } __END__ OUTPUT: Key Candidate Reference the 7 3 cat 0 1 is 0 1 mat 0 1 on 0 1