my %hash; while () { my $clean = ... convert $_ into its canonical form ... $hash{$clean} = 1; } while () { my $clean = ... convert $_ into its canonical form ... if ($hash{$clean}) { # line from file2 exists in file1 } else { # line from file2 is not in file1 } }