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

Hello Monks!
So, it seems like a very trivial problem for me, but my code so far is not working properly.
What I want to do is, supposedly you have two numerical ranges, like:
117-137 AND 112-130

and you want to check whether the second one has an overlap of at least 5 positions with the first one (at least 5 positions must be included in the first one).
How do you proceed? I tried:
$flag_correct=0; if( ($second_beg>=($first_beg-5)) && ($first_beg<=($second_beg+5)) && +($first_end>=($second_end-5)) && ($first_end<=($second_end+5)) ) {$fl +ag_correct = 1;}

but this doesn't work correctly, because in the case mentioned above the flag remains 0.

Replies are listed 'Best First'.
Re: Calculate comparison between
by zwon (Abbot) on Jul 27, 2013 at 15:46 UTC
    ($second_beg>=($first_beg-5)) && ($first_beg<=($second_beg+5))
    This is the same condition written in two different ways. I think you want something like this:
    use List::Util qw(min max); ...; my $overlap = min($first_end, $second_end) - max($first_beg, $second_b +eg) + 1; if($overlap >= 5) { ... };
      Great thanks! Also I realised I was a bit lazy and did not look into Perlmonks older entries, so I found also many entries!
      Thank you very much!
Re: Calculate comparison between
by Laurent_R (Canon) on Jul 27, 2013 at 17:12 UTC

    Just another way perhaps simpler (untested):

    my $max_inf = $first_beg > $second_beg ? $first_beg : $second_beg; my $min_sup = $first_end < $second_end ? $first_end : $second_end; if ($min_sup - max_inf >= 5) { #...

    Update Jul 27, 2013 at approx. 22:20 UTC: I must admit that when I posted this, I had not seen (or not read carefully enough) the proposal made by zwon, but I had only read his comment on wrong boolean test. Looking back to my post and zwon's post, I must say that my proposal is essentially using the same idea and does not bring very much more, except that it does not use a module. I do not regret having posted my solution, but I regret not having seen and therefore not having said upfront that my solution was in fact a variation on the solution posted by zwon.

Re: Calculate comparison between
by Anonymous Monk on Jul 28, 2013 at 02:35 UTC