Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: duplicates getting omitted while comparing values inside foreach.

by ELISHEVA (Prior)
on Apr 16, 2009 at 07:48 UTC ( [id://757878]=note: print w/replies, xml ) Need Help??


in reply to duplicates getting omitted while comparing values inside foreach.

Btree (or lack thereof) isn't the reason for this bug. The bug is in the placement of your print OUT statements. If you clean up the indenting you will notice that lines only print out if

  • ! defined($variant)
  • defined($variant) && ($flag_left==1) && ($flag_right==1)

On the rows where it fails to print out, I presume that $variant is defined, but either the left or right flag is something other than 1.

To fix this I recommend that you move the code for calculating the final column into a separate sub that returns either the file name or "-". Then your loop to read in file 2 should (pseudo code) look something like this:

while (my $line = <DATA>) { my @aFields=split(/\s+/,$line); my $kDb= $aFields[0] . '#' . $aFields[1]; my ($variant,$rs) = split('##', $hash1{$kDb}); #code to calculate my $result=calcFinalColumnValue($variant,$rs,\@aFields); #print row print "@aFields $result\n"; }

Also, you might want to consider two changes to make this code more efficient.

  • use while (<$fh2>) rather than foreach. I believe (someone correct me if I'm wrong), that foreach will cause the entire file to be slurped into an array before the loop starts. This could be a problem in a very large file and defeats the purpose of using a tied hash.
  • for the values of %hash1 use an array reference [$variant, $rs] rather than the string "foo##baz". This will eliminate the need to concatenate hash values when reading in file1 and the need to parse them when reading in file2.

Best, beth

Replies are listed 'Best First'.
Re^2: duplicates getting omitted while comparing values inside foreach.
by patric (Acolyte) on Apr 16, 2009 at 09:36 UTC
    i had missed an else statement after if.
    if(defined($variant)){ foreach my $lis(@snplist){ if($queryinfo[2] eq $lis){$flag_left=1;} elsif($queryinfo[3] eq $lis){$flag_right=1;} } if(($flag_left == 1) && ($flag_right == 1)){ print OUT "$_\t$rs\n"; $c++; $flag_left=0;$flag_right=0; } else{ print OUT "$_\t-\n"; } } else{ print OUT "$_\t-\n"; }
    thank u :)
Re^2: duplicates getting omitted while comparing values inside foreach.
by patric (Acolyte) on Apr 16, 2009 at 09:20 UTC
    thank you so much for your valuable suggestions :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://757878]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-03-29 13:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found