CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Searching hash for values that make part of a key

 



bossanova352
New User

Mar 14, 2013, 8:26 PM

Post #1 of 4 (426 views)
Searching hash for values that make part of a key Can't Post

Hi all,

I'm trying to search through a hash for numbers that correspond to the last few numbers in each string in the keys. The keys look something like this (yes it's a fasta):

>ID_#_length_#_cov_#

I want to filter by the cov_#, and I've gotten so far as to separate these cov_#'s out and can filter them alone. However, I'd like to search through the keys in the hash for the cov_#'s and print out both the keys and values that are left over after filtering the cov_#'s. Here is what I have so far (%sequences is the hash that already has appropriate keys and values):

Code
foreach $header (%sequences){ 
$header =~ /cov_([0-9]*\.[0-9]*)/;
$cov = $1 * 1.0;
if ($cov <= 50 && $cov >= 5){
#print $cov, "\n";
}
}

Any help would be greatly appreciated!


Kenosis
User

Mar 14, 2013, 9:21 PM

Post #2 of 4 (420 views)
Re: [bossanova352] Searching hash for values that make part of a key [In reply to] Can't Post

Perhaps the following will be helpful:

Code
use strict; 
use warnings;

my $cov;

for my $header ( keys %sequences ) {
if ( ($cov) = $header =~ /cov_([\d.]+)/ and $cov <= 50 and $cov >= 5 ) {
print "$header => $sequences{$header}\n";
}
}



(This post was edited by Kenosis on Mar 14, 2013, 9:22 PM)


bossanova352
New User

Mar 14, 2013, 9:38 PM

Post #3 of 4 (416 views)
Re: [Kenosis] Searching hash for values that make part of a key [In reply to] Can't Post

That worked beautifully! Thanks a lot for the help!


Kenosis
User

Mar 14, 2013, 9:54 PM

Post #4 of 4 (414 views)
Re: [bossanova352] Searching hash for values that make part of a key [In reply to] Can't Post

You're most welcome!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives