Just for fun, using an array slice instead of a loop (quickly tested under the Perl debugger):
main::(-e:1): 42 DB<1> $input = "The quick red fox jumped over the lazy brown dog."; DB<2> $spec = "3-6,9-13,17-20"; DB<3> $spec =~ s/\-/../g; DB<4> @in = split //, $input; DB<5> print "@in"; T h e q u i c k r e d f o x j u m p e d o v e r t h e l +a z y b r o w n d o g . DB<6> print @in[eval ($spec)]; qui red jum DB<7>
Just 3 lines of code besides the initializations.

Now, of course, since array subscripts start at 0, I am counting the letters from 0. If we need counting letters from 1, not 0, we can just add an empty element at the beginning of the array:

DB<7> unshift @in, ""; DB<8> print @in[eval ($spec)]; e quk redx ju

Je suis Charlie.

In reply to Re: substring within range by Laurent_R
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.