in reply to Check If 2 Arrays Match
... with filenames that are based on those numbers.
If the derivation of filename from ID is predictable, it might be more efficient to create a hash of filenames from the IDs then use grep to select appropriate files from your @files array rather than keep looping over files and IDs.
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;
The output.
XYZ_dr90324.txt XYZ_jc824.txt XYZ_pj7023.txt
It might be that the filename can't be confidently derived from the ID and this method will not work but I thought I'd post the idea just in case.
Cheers,
JohnGG
|
|---|