http://www.perlmonks.org?node_id=471023
Description: This will find the intersection of two sets. That, by it self, is nothing new... my problem however is space vs. time. I don't want the trade-off, I want it to be fast and small whereas the CPAN modules providing the means I want are always small && slow OR huge && fast I use this to apply blacklists to address-files. The below snippet is a fully functional program. Play around with $keyLength to see increase in performance (or not)
open(local *BLACKLIST, "<blacklist");
open(local *ADDRESS, "<address");

@blacklist = <BLACKLIST>;
@address = <ADDRESS>;

my $sep = "|";
my $keyLength = 6;

my @blackListed = intersection(\@blacklist, \@address);

print "Found: " . scalar(@blackListed) . "\n";

sub intersection {
  my ( $list1, $list2 ) = @_;

  my %strings;
  my $loop;

  # turn the biggest list into a strings hash.
  # AND loop through the smallest list.
  if ( $#{$list1} > $#{list2} ) {
    %strings = makeStringsHash( \$list1 );
    $loop = \$list2;
  } else {
    %strings = makeStringsHash( \$list2 );
    $loop = \$list1;
  }

  # run through the smallest of lists
  my @intersection = ();

  # for each key remember the last position ( the strings in the hash
  # are sorted, remember? )
  my %lastPos = ();
  foreach my $entry ( @{ $$loop } ) {

    my $key = substr($entry, 0, $keyLength);

    my $pos = $lastPos{$key} || 0;
    my $tmp = index( $strings{$key}, $sep.$entry.$sep, $pos );

    # if we found it in the big-list, add it to the intersection
    if ( $tmp != -1 ) {
      push @intersection, $entry;
      $lastPos{$key} = $tmp;
    }
  }

  return @intersection
}

sub makeStringsHash {
  my ( $list ) = @_;

  my %strings = ();
  $strings{substr($_, 0, $keyLength)} .= $sep . $_ . $sep foreach ( so
+rt @$$list );

  return %strings;
}