Others have addressed your immediate problem, but there is plenty of other help to be provided. Consider the following:

#!/usr/bin/perl use strict; use warnings; my $tax2locus_file; my %tax2loc; open my $in, '<', $tax2locus_file or die "Can't open $tax2locus_file: +$!\n"; while (<$in>) { chomp; my ($taxid, $locus) = split /\t/; $tax2loc{$locus} = $taxid; } close ($in); print "there are\t" . keys (%tax2loc) . "\tlocus_ids as key in hash\n" +; ############### Now read in sharedTab file with pairwise overlap info my $sharedTab_file = $ARGV[0]; my $outfile = "$sharedTab_file.hostinfo"; open my $out, '>', $outfile or die "Can't create $outfile: $!\n"; open $in, '<', $sharedTab_file or die "Can't open $sharedTab_file: $!\ +n"; print $out "#prophageA\tprophageB\thostA\ttaxidA\thostB\ttaxidB\tjacc\ +n"; while (<$in>) { chomp; next if (/^#/); # ignore comments my @columns = split (/\t/, $_); my ($prophageA, $hostA, $taxidA) = getTaxId($columns[0]); my ($prophageB, $hostB, $taxidB) = getTaxId($columns[0]); print $out join ("\t", $prophageA, $prophageB, $hostA, $taxidA, $hostB, $taxidB, $col +umns[5]), "\n"; } sub getTaxId { my ($prophage, $lu) = @_; my ($host, $PFnum) = split /\./, $prophage; ## for wgs genomes just match first 7 characters as only NZ_XXXX00 +0000 are ## in tax2locus $host =~ s/^(NZ.{5}).*/$1/; my @matches = grep {$_ =~ /$host/} keys %$lu; die "Expected exactly one match for $host. Got " . scalar @matches + . "\n"; return $prophage, $host, $matches[0]; }

Note that the code is completely untested so may suffer from typos and egregious errors of all sorts, however points to note are:

Note that this code doesn't check to ensure the input data are correctly formatted as I'm not entirely sure what the format ought to be, but "production" code would ensure that sensible values were passed into getTaxId for $prophage for example.

True laziness is hard work

In reply to Re: grep keys in hash and retrieve values by GrandFather
in thread 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.