Hi, along Corion's suggestion but with a little bit of naive range-merging. HTH, although it looks a little bit C-ish...

use strict; use warnings; # valid ranges: 0..$max_line my @err_ranges = qw(39887-399900 23-900 8000-10000 50000-1000500 40000 +-90000); my $max_line = 100000000; # save b(egin) and e(nd) of ranges and sort by lower b(egin) of ranges my @errs_b_e = sort { $a->[0] <=> $b->[0] } map { m/(\d+)\-(\d+)/; [ +$1 , $2 ] } @err_ranges; # merge overlapping and adjacent ranges for (my $i=0; $i<$#errs_b_e; $i++) { my $cmp_a = $errs_b_e[$i]; my $cmp_b = $errs_b_e[$i+1]; my ($a_lo, $a_hi, $b_lo, $b_hi) = map { ($_->[0], $_->[1]) } ($cmp_a +, $cmp_b); #overlapping? e.g. 10..20 15..25 --> 10..25 if ($a_lo <= $b_lo and $b_lo <= $a_hi) { $cmp_b->[0] = $a_lo; # lower bound defined by $a_lo $cmp_b->[1] = $a_hi > $b_hi ? $a_hi : $b_hi; # upper bound defined + by max. $cmp_a->[0] = -1 # taint LHS range } elsif ($a_hi == $b_lo-1) { #adjacent? e.g. 10..20 21..22 --> 10. +.22 $cmp_b->[0] = $a_lo; # lower bound defined by $a_lo $cmp_a->[0] = -1 # taint LHS range } #update/hint: the if/elsif above can be reduced ;-) } # remove tainted ranges, create list of inverse boundaries my @merged = map { $_->[0]-1 => $_->[1]+1 } grep { $_->[0] >= 0 } @e +rrs_b_e; # edge-cases $merged[0] < 0 ? shift @merged : unshift @merged, 0; $merged[-1] > $max_line ? pop @merged : push @merged, $max_line +; for (my $i=0; $i<@merged; $i+=2) { print $merged[$i], "-", $merged[$i+1], "\n"; }
Prints:
pb> perl 752436.pl 0-22 901-7999 10001-39886 1000501-100000000


In reply to Re: greping big numbers by Perlbotics
in thread greping big numbers by baxy77bax

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.