Just for my own fun..

perl -le "for($ARGV[0]=~/\-/g){my@a;map{$a[$_]++for(eval s/\-/\.\./r)} +map{s/^([^,]*),?//;$1}@ARGV;print+(join'-',(grep$a[$_]==2,0..$#a)[0,- +1])}" "25-40,74-93,95-120,130-149" "31-47,84-99,107-123,137-151" 31-40 84-93 107-120 137-149

L*

PS this should be the sub to add to the above bench ( UPDATE it had many errors: corrected)

sub _oneliner{ my @res; for($TM_part1=~/\-/g) { my @arr; # map{ $arr[$_]++ for (eval s/\-/\.\./r) } map{ s/^([^,]*) +,?//; $1 }$TM_part1, $TM_part2; # semplified a bit, see below map{s/^([^,]*),?//;$arr[$_]++ for( eval $1 =~ s/\-/\.\./r +)}$TM_part1,$TM_part2; push @res,(join'-',(grep { defined $arr[$_] and $arr[$_] = += 2 }0..$#arr)[0,-1] ); } return join ',', @res; } __END__ 1..4 ok 1 ok 2 ok 3 ok 4 Rate Span Perl Pair Oneliner Span 12055/s -- -27% -84% -100% Perl 16553/s 37% -- -79% -100% Pair 77602/s 544% 369% -- -99% Oneliner 11429533/s 94715% 68949% 14628% --

UPDATE the double map is really redundant and unneeded in the above sub:

# map{$arr[$_]++ for(eval s/\-/\.\./r)}map{s/^([^,]*),?//;$1}$TM_part1 +,$TM_part2; # should be: map{s/^([^,]*),?//;$arr[$_]++ for( eval $1 =~ s/\-/\.\./r )}$TM_part1, +$TM_part2; # so also the oneliner can be reduced: perl -le "for($ARGV[0]=~/\-/g){my@a;map{s/^([^,]*),?//;$a[$_]++for eva +l$1=~s/\-/\.\./r}@ARGV;print join'-',(grep$a[$_]==2,0..$#a)[0,-1]}" "25-40,74-93,95-120,130-149" "31-47,84-99,107-123,137-151" 31-40 84-93 107-120 137-149

PS I fixed 1..$#a with 0..$#a in the above code snippets

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to Re: Create union from ranges, but compare respectively -- oneliner by Discipulus
in thread Create union from ranges, but compare respectively 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.