I have the following data structure:
my %hash = (
'hsa_circ_0024017|chr11:93463035-93463135+|NM_033395|KIAA1731 FORWARD' => [
{
'energy' => '-4.3',
'spacer' => 'AGGCACC',
'end' => '97',
'start' => '81'
}
],
'hsa_circ_0067224|chr3:128345575-128345675-|NM_002950|RPN1 FORWARD' => [
{
'energy' => '-4.4',
'spacer' => 'CAGT',
'end' => '17',
'start' => '6'
},
{
'energy' => '-4.1',
'spacer' => 'GTT',
'end' => '51',
'start' => '26'
},
{
'energy' => '-4.1',
'spacer' => 'TTG',
'end' => '53',
'start' => '28'
}
],
...
);
How do I access the contents of my hash to be able to compare the contents within a loop?
For each parent hash (hsa_circ...) I want to compare the child hashes (spacers) together. Forgive me I'm struggling to word this right. This is a small sample of the data of course. My goal, in brief, is to detect the arrays of hashes which have the same spacer and if they do have the same spacer then I want to then choose the array of hashes which has the lowest energy score.
The problem: there may be groups of hashrefs in each arrayref with the equal spacer value. In each such group the hashref with the lowest energy value need be identified, to replace that group.
Most work is done in
partition_equal()
, which identifies hashref groups with equal spacersOutput
The order inside arrayrefs is not maintained; the new arrayref has first hashrefs with unique spacer values, then those with lowest-energy value (for each original group with same spacer-values).
The sub sorts input by spacer values, so that it can identify equal ones by simply iterating through the sorted array and comparing only neighbors. This should be reasonably efficient.
Like this
so it's $this{hash}->[array_ref]{hash]
or
will get you (the reference to) the array of (references to) spacer definitions for each line in turn. Then, you can edit that array as follows:
Notes:
The order of the "spacer definitions" may change. If that's a problem, replace
with
In case of two spacer definitions with the same spacer and the same energy, the first one encountered is kept.
Not the answer you're looking for? Browse other questions tagged arrays perl hashmap perl-data-structures or ask your own question.