Strictures are your friend (use strict; use warnings; - see The strictures, according to Seuss). In this case forcing you to declare variables with my may have made you think about the scope of $u_set and that may have led you to realize that the print is in the wrong place, and that may have led you to realize that the for loop was only performing one iteration, and that may have led you to notice that you used a tab character to split on instead of a comma. A better version of the code might look like:

use strict; use warnings; 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); for my $i (0 .. $#split_TM1) { my $set1 = Set::IntSpan->new($split_TM1[$i]); my $set2 = Set::IntSpan->new($split_TM2[$i]); my $u_set = intersect $set1 $set2; print "Union of strings: $u_set\n"; }

Note the Perlish for loop and avoidance of object indirect calling of the Set::IntSpan constructor.

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

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