use strict; use warnings; my @IDs = qw{ pj7023 ge4872 dr90324 jc824 }; my @files = qw{ XYZ_dr90324.txt XYZ_at728.txt XYZ_jc824.txt XYZ_uf72082.txt XYZ_tc43.txt XYZ_pj7023.txt XYZ_yj82195.txt XYZ_fw607.txt }; my %IDnames = map { my $key = q{XYZ_} . $_ . q{.txt}; $key => 1 } @IDs; my @selectedFiles = grep exists $IDnames{ $_ }, @files; print qq{$_\n} for @selectedFiles; #### XYZ_dr90324.txt XYZ_jc824.txt XYZ_pj7023.txt