erons has asked for the wisdom of the Perl Monks concerning the following question:

I have 2 best scores, and for each best score I have 3 possiblities: "unique match", "best_match+TE" and "best_match-TE". Then I need to compare/check the agreement(or otherwise) between each of the possibilities in both "best scores". I have tried this, but I am not sure if I am in right direction

use strict; if($best_X <= 110 && $block1 <=110 || $best_Y <= 110 && $block2 <= 110 + )<p>{#keep counts of matches to check for unique match $counter++;}</p> if($counter == 1){ $tq = "unique_match" }elsif($best_X <= 100 && $block1 <= 100)</p>{ $pp = "best_match+TE" }elsif($best_X <= 100 && $position2 < 5)</p>{ if($block2 <= 50){ $pp2 = "best_match-TE"}<p> }else{next} if($best_Y <= 100 && $block1 >= 70){ $pz = "best_match+TE" }<p>elsif($best_Y <= 100 && $position1 >= 50)<p>{ if($block1 <= 50){ $pz2 = "best_match-TE"} }else{next}<p> } if($pp eq $pz){ $resH = "best_match+TE"; }<p>elsif($nTE2 eq $pp2 || $nTE2 eq $pz2){ $resH = "best_match -TE" }else{$resH = $tq}

Replies are listed 'Best First'.
Re: I need help with a bunch of "if/elsif statements (comparisons)"
by wfsp (Abbot) on Jan 27, 2010 at 17:19 UTC
    I can't help with algorithm either but I would offer some observations.

    You would use a next statement if your code is in a while or for loop. Is this the case?

    On a minor note, you test $best_X <= 100 twice. You could refactor it to only test it once. If the condition changes you only have one thing to change (same applies to $best_Y).

    In your $best_X tests you test $block1 and $block2 and in the $best_Y tests you test $block1 twice. Is that what you intended? This may be an over enthusiastic search for symetry on my part. :-)

    There appears to be an unmatched closing brace.

    But, but, but... The main thing is to test it rigourously. With all possible values for every variable and in all possible combinations. And then some. Then you'll know if you are heading in the right direction.

    I've taken the liberty of tweaking the format a bit.

    #! /usr/bin/perl use strict; use warnings; my ( $best_X, $best_Y, $block1, $block2, $counter, $tq, $pp, $pp2, $pz, $pz2, $position1, $position2, $resH, $nTE2 ); #keep counts of matches to check for unique match $counter++ if ( $best_X <= 110 && $block1 <= 110 || $best_Y <= 110 && $block2 <= 110 ); if ($counter == 1){ $tq = "unique_match"; } elsif ($best_X <= 100){ if ($block1 <= 100){ $pp = "best_match+TE"; } elsif ($block2 <= 50 && $position2 < 5){ $pp2 = "best_match-TE"; } } else{ next; # are we in a loop? } if ($best_Y <= 100){ if ($block1 >= 70){ $pz = "best_match+TE"; } elsif ($block1 <= 50 && $position1 >= 50){ # block2? $pz2 = "best_match-TE"; } } else{ next; } #} perl complains about this one $resH = $tq; if ($pp eq $pz){ $resH = "best_match+TE"; } elsif ($nTE2 eq $pp2 || $nTE2 eq $pz2){ $resH = "best_match -TE" }
Re: I need help with a bunch of "if/elsif statements (comparisons)"
by jethro (Monsignor) on Jan 27, 2010 at 16:41 UTC

    Those <p> tags in your code and/or your formatting leave your script quite unreadable.

    I also can't decipher from your description what exactly you want to do. What are the input values $best_X, $best_Y, $block1, $block2, $position2, position1 ... and how are they related to each other? What do the numbers 110, 100 and 70 have to do with your problem?

      Hi, the numbers: 110, 70, 100, etc relate to the different cut-off at which I want to call bestmatch+TE or bestmatch-TE or unique match. I need to do this for two diffrent IDs(have the best scores) and then compare the agreement between them. For example, if best_X gives bestmatch+TE, does best_Y also give the same result? my aim was to keep the call in $resH

        Ok, I'm still not sure what exactly should happen but it seems you first want to classify both ids in the same way. This is a job for a subroutine, so that you don't have to do that twice:

        sub award_score { #awards one of three scores to a tupel of # best result,block and position. The scores are # ... my ($best,$block,$position)= @_; my $result="nothing"; if ($best<100 and $block<100) { ... $result='best_match-TE' } elsif { ... $result= ... } return $result; }

        Now that you have a well defined and (hopefully) well documented subroutine to do the preliminary work on each individual score, you can add the main code:

        my $result_x= award_score($best_x,$block1,$position1); my $result_y= award_score($best_y,$block2,$position2); if ($result_x eq $result_y) { ... }

        This is only a rough draft that you have to adapt, it is just to show you the principle. Somehow or other a similar method should work for you. If you need more parameters into the subroutine, just do that. If you need to return more than one result, likewise no problem. If your main routine looks so simple that someone else could understand it immediately you have won

        PS: variable names like $nTE2 are cryptic and not very helpful if you or someone else has to understand the code one year later

Re: I need help with a bunch of "if/elsif statements (comparisons)"
by JavaFan (Canon) on Jan 27, 2010 at 16:45 UTC
    You're showing us badly formatted code, of which you aren't sure it's right. You also describe what you have (although that doesn't seem to match the code). But you aren't saying what you want to achieve.

    So, how do you expect to get a useful answer?

Re: I need help with a bunch of "if/elsif statements (comparisons)"
by Anonymous Monk on Jan 27, 2010 at 16:20 UTC
    I know this isn't very helpful, but if you could provide a little context and better formatting, we might be able to help factor that down a little. For example, I'm guessing some of these strings are going to be used after this bit of code, so I'm not sure which ones need to keep their values and which are just there for later comparisons, which could be written in many ways.

    As it is, if it works, use it. If it's slow, benchmark it (and compare it with other code in the program to identify the bottleneck). Only then can you really determine whether changing it buys you anything.

      Hi, I only need to keep and print $resH, which holds the string after the final 'comparison'

      use strict; if($best_X <= 110 && $block1 <=110 || $best_Y <= 110 && $block2 <= 110 + ){#keep counts of matches to check for unique match $counter++;} if($counter == 1){ $tq = "unique_match" }elsif($best_X <= 100 && $block1 <= 100){ $pp = "best_match+TE" }elsif($best_X <= 100 && $position2 < 5){ if($block2 <= 50){ $pp2 = "best_match-TE"} }else{next} if($best_Y <= 100 && $block1 >= 70){ $pz = "best_match+TE" }elsif($best_Y <= 100 && $position1 >= 50){ if($block1 <= 50){ $pz2 = "best_match-TE"} }else{next} }

      It also looks like the OP has HTML tags mixed into the code.

      Erons: Copy and paste the code directly from your source into the <code></code> tags to prevent any typos and ensure that the code will still work.