#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my @items; foreach my $key (keys %hash){ push(@items,$key); } my $rxMatchItems; { local $" = q{|}; $rxMatchItems = qr{(?:@items)}; } open(FH,'<','gene_association.goa_uniprot') or die "Can not open/access 'gene_association.goa_uniprot'\n$!"; while(){ next unless m{$rxMatchItems}; print $_; } close(FH);