Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Create union from ranges, but compare respectively -- oneliner

by Discipulus (Canon)
on Jun 10, 2022 at 12:13 UTC ( [id://11144650]=note: print w/replies, xml ) Need Help??


in reply to Create union from ranges, but compare respectively

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11144650]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-20 03:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found