my %file1; my %file2; while() { my $field_from_1 = (split //)[1]; $file1{$field_from_1}++; } while() { my ($field_from_2, $record_we_want) = (split //)[4,index_we_cant_guess_from_description]; $file2{$field_from_2} = $record_we_want; } foreach my $keys ( %file2 ) { if ( exists $file1{$key} ) { # do whatever it is they want with $record_we_want } }