# 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 } } } #### # 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 .= $_; } } #### sub read_entry { my $FH = shift; my @value_arr; while (<$FH>) { return \@value_arr if /EOE/; chomp; # might not be needed, will depend on what will be done next with the array of lines push @value_arr, $_; } }