Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Create union from ranges, but compare respectively

by hv (Prior)
on Jun 10, 2022 at 01:08 UTC ( [id://11144631]=note: print w/replies, xml ) Need Help??


in reply to Create union from ranges, but compare respectively

It looks like your strings are comma-separated, but you are splitting them on tabs. You are also overwriting $u_set each time round the loop, so you'll need to accumulate the result differently.

I'd probably aim to write it something like this:

use strict; # always use warnings; # always use Set::IntSpan; my $TM_part1 = "25-40,74-93,95-120,130-149"; my $TM_part2 = "31-47,84-99,107-123,137-151"; my @split_TM1 = split ',', $TM_part1; my @split_TM2 = split ',', $TM_part2; my $u_set = join ',', map { my $set1 = Set::IntSpan->new($split_TM1[$_]); my $set2 = Set::IntSpan->new($split_TM2[$_]); intersect $set1 $set2; } 0 .. $#split_TM1; print "Union of strings: $u_set\n";

.. but if you're not comfortable with map, you can accumulate the results in an array instead:

use strict; # always use warnings; # always use Set::IntSpan; my $TM_part1 = "25-40,74-93,95-120,130-149"; my $TM_part2 = "31-47,84-99,107-123,137-151"; my @split_TM1 = split ',', $TM_part1; my @split_TM2 = split ',', $TM_part2; my @u_set; for my $i (0 .. $#split_TM1) { my $set1 = Set::IntSpan->new($split_TM1[$i]); my $set2 = Set::IntSpan->new($split_TM2[$i]); push @u_set, intersect $set1 $set2; } my $u_set = join ',', @u_set; print "Union of strings: $u_set\n";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-16 23:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found