in reply to matching data with 2 files

sorry guys here is the corrected file (copy paste is not so good



open (LIST1, $list1) or die "cannot open $list1";
open (LISt2, $list2) or die "cannot open $list2";

while (<LIST1>)
{
#get all the variables for line from LIST1

my ($c,$d,$e,$f,$g,$h) = split(/"\t"/);
$flag=0; #reset the flag

my %list2key= ();
while (<LIST2>)
{
($a, $b) = split(/"\t"/);
$list2key{$a} = $b;
}

if (exists $list2key{$c})
{
$name = $list2key{$c};
#print out to screen as a test then to a file later print "$name $d $e $f $g\n";
$flag=1;
}
}

close LIST2;
close LIST1;

Replies are listed 'Best First'.
Re: Re: matching data with 2 files
by Skeeve (Parson) on Jul 16, 2003 at 12:22 UTC
    open (LIST1, $list1) or die "cannot open $list1"; open (LIST2, $list2) or die "cannot open $list2"; while (<LIST1>) { #get all the variables for line from LIST1 my ($c,$d,$e,$f,$g,$h) = split(/"\t"/); # do you mean "\t" or \t? # are there " in the file? $flag=0; #reset the flag # which flag? my %list2key= (); while (<LIST2>) { ($a, $b) = split(/"\t"/); $list2key{$a} = $b; } # This will read your second file once for the # first line of your first file # As soon as you have the second line of you first # file, it will forget about the second file! # Maybe you should move this loop out of the # LIST1-loop if (exists $list2key{$c}) { $name = $list2key{$c}; #print out to screen as a test then to a file later print "$name $ +d $e $f $g\n"; $flag=1; } }
    Does this help you understanding the script you copied?
      Thanks Skeeve, moving the inside loop out worked
      once i stopped confusing my key and hash values
      below is the final code, a bit clinky but it works


      open (LIST1, $list1) or die "cannot open $list1";
      open (LIST2, $list2) or die "cannot open $list2";

      my %list2key= ();
      while ($line1 = <LIST1>)
      {
      my ($a, $b) = split(/\t/,$line1);
      $list2key{$a} = $b;
      }

      while ($line2 = <LIST2>)
      {
      #get all the variables for line from LIST2

      my ($a, $b, $c, $d, $e, $f) = split(/\t/,$line2);
      # do you mean "\t" or \t? are there " in the file?

      # This will read your second file once for the
      # first line of your first file
      # As soon as you have the second line of you first
      # file, it will forget about the second file!
      # Maybe you should move this loop out of the LIST1-loop

      if (exists $list2key{$a})
      {
      $name = $list2key{$a};
      #print out to screen as a test then to a file later
      print "$name $b $c $d $e $f\n";
      }
      }

      close LIST2;
      close LIST1;