This sound like a home work, you should show us what did you try before

I hope you can understand next code, which do what do you want. See split,perlre, and substr if you need help reading it.
#!/usr/bin/perl use strict; use warnings; my $range="0-2,6-10,13,15"; my $test="this is a test with more than 15 characters"; for my $r (split (",",$range)) { if ($r =~ /^(\d+)(-(\d+))?$/) { my $first=$1; my $length=1; if (defined ($3)) { $length=$3-$1+1; } print "$r - ", substr ($test, $first,$length),"\n"; } }
Updated: For fun,see this example for use regex "...(..)..(..).." style (no substr needed). For "x" usage see Multiplicative Operators:
#!/usr/bin/perl use strict; use warnings; my $range="0-2,6-10,13,15"; my @range=split(",",$range); my $test="this is a test with more than 15 characters"; my $last=0; my $regex=""; for (@range) { if (/^(\d+)(-(\d+))?$/) { my $first=$1; my $length=1; if ($last lt $first) { $regex.="."x($last-$first); } $last=$first; if (defined ($3)) { $length=$3-$1+1; $last=$3; } $regex.="(".("."x$length).")"; } } my @matches = $test =~ /$regex/; my $n=0; for (@matches) { print ++$n." ($range[$n-1]): \"" ."$_","\"\n"; }

In reply to Re: substring within range by i5513
in thread substring within range by smartperl

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.