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