# open your file 1 as $IN1... my %hash_file_1; while (<$IN1>) { if (/entry) { my $key = $_; my $value = ""; while (<$IN1>) { last if /EOE/; $value .= $_; } $hash_file_1{$key} = $value; } } # open file 2 as $IN2 while (<$IN2>) { if (/entry) { my $key = $_; if (exists $hash_file_1{$key}) { my $value2 = ""; while (<$IN2>) { last if /EOE/; $value2 .= $_; } # do the comparison between $value2 and $hash_file_1{$key} # and print what you need } } }