I have two arrays of hashes. I want to narrow down the second one according to variables in the first.
The first array contains hashes with keys seqname
, source
, feature
, start
, end
, score
, strand
, frame
, geneID
and transcriptID
.
The second array contains hashes with keys
organism
, geneID
, number
, motifnumber
, position
, strand
and sequence
.
What I want to do, is remove from the first array of hashes, all the hashes which have a variable geneID
which is not found in any of the hashes of the second array. - Note both types of hash have the geneID
key. Simply put, I want to keep those hashes in the first array, which have geneID
values which are found in the hashes of the second array.
My attempt at this so far was with two loops:
my @subset # define a new array for the wanted hashes to go into.
for my $i (0 .. $#first_hash_array){ # Begin loop to go through the hashes of the first array.
for my $j (0 .. $#second_hash_array){ # Begin loop through the hashes of the 2nd array.
if ($second_hash_array[$j]{geneID} =~ m/$first_hash_array[$i]{geneID}/)
{
push @subset, $second_hash_array[$j];
}
}
}
However I'm not sure that this is the right way to go about this.