use strict; use warnings; use DB_File; my $hashfile = "hash.$$"; tie my %hash1, "DB_File", $hashfile, O_RDWR|O_CREAT, 0666, $DB_HASH or die "cannot open file $hashfile: $!"; open(my $fh1, "<", "file1.txt") or die "file1.txt: $!"; foreach (<$fh1>) { chomp; my @parts = split(/\s+/); $hash1{"$parts[1]#$parts[2]"} = $parts[4]; } close($fh1); open(my $fh2, "<", "file2.txt") or die "file2.txt: $!"; foreach (<$fh2>) { my @parts = split(/[\s>]+/); my $value = $hash1{"$parts[0]#$parts[1]"}; if( defined($value) and grep { $_ eq $parts[2] } split(/\//, $value)) { print "$_"; } } close($fh2); untie %hash1; unlink($hashfile);