Apparently you're right on both accounts. That of the nested range is a stupid mistake of mine, thinking that the end of the second range would always be the larger one because its start is. In fact, what I had in my head before I wrote anything down, was using max():
use List::Util qw(max); $e = $range[$i][1] = max($e, $range[$j][1]);
But in fact, your code is simpler, thus, better.

But that of the infinite loop surprised me, a lot. I had assumed that the condition, the second expression in the for(;;) line, was being tested before any attempt at running the loop body, for each loop.

print "before\n"; for(my $i = 5; $i < 2; $i++) { print " # $i\n"; } print "after\n";
Result:
before after
But apparently, something goes wrong if you do a redo:
print "before\n"; for(my $i = 5; $i < 10; $i++) { print " # $i\n"; if($i==7) { $i = 12; redo; } } print "after\n";
Result:
before # 5 # 6 # 7 # 12 after
Ouch!

Hmm... perldoc -f redo apparently says:

The "redo" command restarts the loop block without evaluating the conditional again. The "continue" block, if any, is not executed.
I recalled the latter, but forgot about the former. I guess it applies here as well. But what seems odd: the variable is initialized again, but the condition is not tested? Heh? While for the first loop, it is initialized and then tested? That is awkward.

I haste to say this... but a goto almost seems better:

for (my $i = 0; $i < @range; $i++) { REDO: my $e = $range[$i][1]; for(my $j = $i+1; $j < @range; $j++) { unless($range[$j][0] > $e) { $range[$i][1] = $range[$j][1] if $range[$j][1] > $e; splice @range, $j, 1; goto REDO; } } }
Much as I despise goto, it seems like using redo as in your code, with the extra condition, is actually worse. Well, I can work around it:
@range = sort { $a->[0] <=> $b->[0] } @range; for (my $i = 0; $i < @range; $i++) { CURRENT: { my $e = $range[$i][1]; for(my $j = $i+1; $j < @range; $j++) { unless($range[$j][0] > $e) { $range[$i][1] = $range[$j][1] if ($range[$j][1] > $e); splice @range, $j, 1; redo CURRENT; } } } }
which seems OK.

In reply to Re: Re: Re: Overlapping portions of sub strings by bart
in thread Overlapping portions of sub strings by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.