#!/usr/bin/perl -w use strict; use warnings; my %href; open my $fh, "<", "File1.txt" or die "Cannot open File1: $!"; while (<$fh>) { chomp; push @{$href{$1}}, $2 if /(\S+)\s+(\S+)/; } while (my ($key, $value) = each(%href)) { print "$key, (@{$value})\n"; } close $fh; open $fh, "<", "File2.txt" or die "Cannot open File2: $!"; { local $/ = '>'; while ( <$fh> ) { chomp; next unless ( s{ \A (\S+) \s+ (?= \d ) }{}xms and exists( $href{$1} )); my $name = $1; my @numbers = split /\D+/; for my $index (@{$href{$name}}) { next if $index > $#numbers; if ($numbers[$index] >= 20) { print "$name\t\t$index\t$numbers[$index]\n"; } } } }