
gudhead
Novice
Dec 31, 2009, 9:09 AM
Post #1 of 1
(449 views)
|
|
Building and retrieving data from array of hashes
|
Can't Post
|
|
I have a file which is tab delimited(a hit from a DB). I would like to first store say the first and tenth item on each(row) as well as the entire row in a hash. And then build an array of hashes from 2nd,3rd,...... rows respectively. I would then sort each of the rows by score and ID(which are the first and 10th elements of each row) and print the row with the best score. I have 2 different IDs in my hits and I am trying to retrieve best hit for each ID based on best scores as in my post before this. I have written this perl code: use strict; use warning; my @hITS; while (<CBLAT>) { chomp; my %row; @row{qw( score qID )} = ( split /\t/ )[ 0, 9 ]; push @hITS, \%row; } @hITS = sort { $a->{qID} cmp $b->{qID} || $b->{score} <=> $a->{score} } @hITS; I need to print, for instance ; "scorea" : "IDx" and "scoreb": "IDy"; so, I tried this: $score = $hITS[0]{score}; $Id = $hITS[0]{qID}; for (@hITS){ if ($_ !~ /$id/ && $_->{score} <= $score){ $_->{qID} =$id1 ; $_->{score} = $score1;} I need some direction on exactly how to get the best hit for each ID based on best score. SAMPLE INPUT DATA: score qsize strand qID 80 90 + pwx 75 90 - pwy 63 90 - pwy 81 90 + pwx
(This post was edited by gudhead on Dec 31, 2009, 9:38 AM)
|