Thank you bichonfrise74. Your suggestion works very well. It is much simpiler and quite a bit faster than what I was using:

#!/usr/bin/perl use warnings; use strict; my $idfile = 'C:...'; open(ID, $idfile) or die "Cannot open file '$idfile' \n\n"; #Create and open output file my $out = 'C:\Users\Clayton\Documents\Research\GO\Data\chr_8\chr_8_pos +gen'; open (OUT, ">$out"); # Create Variables my $line; my $line2; my %hash = (); # Read in file line by line while ( $line = <ID> ) { chomp $line; # Split the tab delimited file into an array my @arr = split /\t/, $line; #if(defined($arr[2])){ # Put the columns of the array into variables my $id = $arr[0]; my $catg_id = $arr[1]; my $pos = $arr[2]; $hash{$id} = "$catg_id\t$pos"; } close ID; my @k = keys %hash; my $k; my $namefile = 'C:...'; open(NAMEFILE, $namefile) or die "Cannot open file '$namefile' \n\n"; while ( $line2 = <NAMEFILE> ) { chomp $line2; my @loc = split /\t/, $line2; foreach $k (@k) { my @arr2 = split /\t/, $hash{$k}; if($loc[0] == $arr2[0]){ if(($arr2[1] >= $loc[1]) and ($arr2[1] <= $loc[2])){ print OUT "$arr2[0]\t$k\t$arr2[1]\t$loc[1]\t$loc[2]\t$ +loc[3]\n" }}}}

I'm still new to perl so I guess a lot of my methods are not the most efficient.

One more question though... Whenever I use your code or mine I get an error "Use of uninitialized value in numeric gt (>)at C:\...line 32, <NAMEFILE> line 1. This error repeats many times for each line in the file. The output is what I expect it to be though and the program runs without any problems (other than generating a bunch of warnings).


In reply to Re^2: Comparing multiple entries from two files by hanger4
in thread Comparing multiple entries from two files by hanger4

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.