sub getBits { # lookup: colour:red -> is id/bitposition:0 # lookup: material:wood -> is id/bitposition:4 } my $bits = getBits('red-wood'); # $bits is 10001 my $nBits = unpack '%32b*', $bits; # http://docstore.mik.ua/orelly/perl/prog/ch03_182.htm : "efficiently counts the number of set bits in a bit vector" for my $straw ( @haystack ){ # loop over all records and compare my $similarity = unpack '%32b*', $straw & needle; # compute a delta print "Percentage similarity %f\n", $similarity / $nBits * 100; # delta in relation to nbits benchmark ("distance") } # then, sort by similarity