Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Hash - Compare - Multiple value keys

by RMGir (Prior)
on Sep 07, 2008 at 11:57 UTC ( [id://709633]=note: print w/replies, xml ) Need Help??


in reply to Hash - Compare - Multiple value keys

From your comments, it sounds like you're unhappy with this loop?
# here is where I messed up for the query. I cannot control this loop. # it finds the things but fails to print them only once. foreach $1 (sort keys %file1){ foreach my $position1 (@{$file1{$1}}){ $found =0; foreach $contig_id(sort keys %hash_1){ foreach my $position (@{$hash_1{$contig_id}}){ $found = 1 if $1 =~ /^$contig_id/ && $contig_id=~ /^$1 +/ && $position1==$position; print RESULTS "$position1\t$1\n" if $found; print "not matched\n" if !$found; } } } }
What I can't understand from either your description OR your code is what you intend to happen here.

As it's written, $found gets set to 0 only in the middle foreach loop, so if it's ever set to 1 in the inner loop, it'll remain 1 for all the rest of the inner loop iterations. Is that what you want?

Try to explain clearly what you want this loop to do, both to yourself and to us. Odds are that explaining it to yourself will be enough for you to figure out a better way to code it, but if it isn't, we're here to help...


Mike

Replies are listed 'Best First'.
Re^2: Hash - Compare - Multiple value keys
by sesemin (Beadle) on Sep 07, 2008 at 17:26 UTC
    Hi Mike, Thanks for the comment. In the inner loop, after the condition is met, I want to print the result only once. then the loop should continue to check other records to see if it can find the match. Here I have included a partial output of this code. I just need to print the first line. where col 1 and 4 pair and 2 and 3 pair. this is were the condition is met. Then it should find the next match before printing ~4000 junk comparisons.
    CLS_S3_Contig10021 349 349 CLS_S3_Contig10021 CLS_S3_Contig10021 898 349 CLS_S3_Contig10021 CLS_S3_Contig10021 524 349 CLS_S3_Contig10021 CLS_S3_Contig10021 365 349 CLS_S3_Contig10021 CLS_S3_Contig10021 373 349 CLS_S3_Contig10021 CLS_S3_Contig10029 857 349 CLS_S3_Contig10021 CLS_S3_Contig10029 482 349 CLS_S3_Contig10021 CLS_S3_Contig10029 676 349 CLS_S3_Contig10021 CLS_S3_Contig10029 153 349 CLS_S3_Contig10021 CLS_S3_Contig10031 797 349 CLS_S3_Contig10021 CLS_S3_Contig10031 587 349 CLS_S3_Contig10021 CLS_S3_Contig10031 227 349 CLS_S3_Contig10021 CLS_S3_Contig10031 314 349 CLS_S3_Contig10021 CLS_S3_Contig10031 605 349 CLS_S3_Contig10021 CLS_S3_Contig10031 257 349 CLS_S3_Contig10021 CLS_S3_Contig10031 212 349 CLS_S3_Contig10021 CLS_S3_Contig10031 857 349 CLS_S3_Contig10021 CLS_S3_Contig10031 635 349 CLS_S3_Contig10021 CLS_S3_Contig10031 188 349 CLS_S3_Contig10021 CLS_S3_Contig10031 410 349 CLS_S3_Contig10021 CLS_S3_Contig10031 806 349 CLS_S3_Contig10021 CLS_S3_Contig10040 439 349 CLS_S3_Contig10021 CLS_S3_Contig10051 719 349 CLS_S3_Contig10021 CLS_S3_Contig10051 92 349 CLS_S3_Contig10021 CLS_S3_Contig1006 279 349 CLS_S3_Contig10021 CLS_S3_Contig1006 240 349 CLS_S3_Contig10021 CLS_S3_Contig1006 168 349 CLS_S3_Contig10021 CLS_S3_Contig10070 196 349 CLS_S3_Contig10021 CLS_S3_Contig10072 882 349 CLS_S3_Contig10021 CLS_S3_Contig10072 685 349 CLS_S3_Contig10021 CLS_S3_Contig10072 892 349 CLS_S3_Contig10021 CLS_S3_Contig10072 237 349 CLS_S3_Contig10021 CLS_S3_Contig10072 858 349 CLS_S3_Contig10021 CLS_S3_Contig10083 868 349 CLS_S3_Contig10021 CLS_S3_Contig1010 774 349 CLS_S3_Contig10021 CLS_S3_Contig1010 613 349 CLS_S3_Contig10021 CLS_S3_Contig10134 452 349 CLS_S3_Contig10021 CLS_S3_Contig10157 545 349 CLS_S3_Contig10021 CLS_S3_Contig10157 500 349 CLS_S3_Contig10021 CLS_S3_Contig10157 875 349 CLS_S3_Contig10021 CLS_S3_Contig10157 404 349 CLS_S3_Contig10021
      So what's the purpose of $found?

      If all you need is to find out if this line matches criteria and do something based on that, that's pretty well what "if" statements are for...

      foreach $1 (sort keys %file1){ foreach my $position1 (@{$file1{$1}}){ foreach $contig_id(sort keys %hash_1){ foreach my $position (@{$hash_1{$contig_id}}){ if($1 =~ /^$contig_id/ && $contig_id=~ /^$1/ && $posit +ion1==$position) { print RESULTS "$position1\t$1\n"; } else { print "not matched\n"; } } } } }

      Mike

Log In?
Username:
Password:

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

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

    No recent polls found