#!/usr/bin/perl use warnings; use English q/-no_match_vars/; use strict; use Carp qw/croak carp/; use Data::Dumper; my $file_one_ref; my $file_one_fn = shift or croak "No input file specifed for file one"; my $file_two_fn = shift or croak "No input file specifed for file two"; open my $file_one_fh, '<', $file_one_fn or croak "Could not open filename [$file_one_fn] : ", $OS_ERROR; while (<$file_one_fh>) { $file_one_ref->{ lc $1 } = $2 if / \A (\w+) \s+ (\w+) /xms; } # You can do the above input like this but it's not as efficient: # map { $file_one_ref->{ lc $1 } = $2 if / (\w+) \s+ (\w+)/xms; } <$input_fh>; # Have a look at the keys read in: print Dumper($file_one_ref), "\n\n"; open my $file_two_fh, '<', $file_two_fn or croak "Could not open filename [$file_two_fn] : ", $OS_ERROR; $INPUT_RECORD_SEPARATOR = '>'; while ( my $line = <$file_two_fh> ) { # I reserve s{}{}xms; for multi line expressions next if ( $line !~ s/ \A (\S+) \s+ (?= \d ) //xms and !exists $file_one_ref->{ lc $1 } ); # why not just use a match? is $name going to contain numbers my $name = lc $1; my $numbers = [ grep { $_ } split /\D+/, $line ]; my $one_number = $numbers->[ $file_one_ref->{$name} - 1 ] || next; if ( $one_number >= 20 ) { print join " ", $name, $file_one_ref->{$name}, $one_number, "\n"; } } close $file_one_fh or croak "Could not open filename [$file_one_fn] : ", $OS_ERROR; close $file_two_fh or croak "Could not open filename [$file_two_fn] : ", $OS_ERROR; __END__