I have a hash that contains locus names as the keys and taxids as the values. I have another list of genomes that are partial matches of the locus names, therefore I want to grep the keys and if there is a match retrieve the corresponding value (taxid). At the moment I think my $matching_key, is returning the number of keys that match the grep and not the actual hash key. Any help with the code is appreciated.

open (IN,$tax2locus_file); while(<IN>){ my($taxid,$locus)=split(/\t/,$_); $tax2loc{$locus}=$taxid; } close(IN); print "there are\t".scalar(keys %tax2loc)."\tlocus_ids as key in hash\ +n"; ############### Now read in sharedTab file with pairwise overlap info my $sharedTab_file=$ARGV[0]; my @columns; my $prophageA; my $prophageB; my $outfile="$sharedTab_file.hostinfo"; my $hostA; my $PFnumA; my $hostB; my $PFnumB; my $regex; my $matching_key; my $taxidA; my $taxidB; open (OUT,">$outfile"); open(IN,$sharedTab_file); print OUT "#prophageA\tprophageB\thostA\ttaxidA\thostB\ttaxidB\tjacc\n +"; while(<IN>){ chomp; next if (/^#/); # ignore comments @columns=split(/\t/,$_); $prophageA=$columns[0]; ($hostA,$PFnumA)=split(/\./,$prophageA); if ($hostA =~ /^NZ/){ ## for wgs genomes just match first 7 characters + as only NZ_XXXX000000 are in tax2locus my $hostA=substr $hostA, 0, 7; } $regex=qr/$hostA/; $matching_key=grep { $_ =~ /$regex/ } keys %tax2loc; $taxidA=$tax2loc{$matching_key}; $prophageB=$columns[1]; ($hostB,$PFnumB)=split(/\./,$prophageB); if ($hostB =~ /^NZ/){ ## for wgs genomes just match first 7 characters + as only NZ_XXXX000000 are in tax2locus my $hostB=substr $hostB, 0, 7; } $regex=qr/$hostB/; $matching_key=grep { $_ =~ /$regex/ } keys %tax2loc; $taxidB=$tax2loc{$matching_key}; my $jacc=$columns[5]; print OUT join("\t",$prophageA,$prophageB,$hostA,$taxidA,$hostB,$taxid +B,$jacc)."\n";

In reply to grep keys in hash and retrieve values by AWallBuilder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.