It's not clear exactly where you expect your output to be. You're calling substr inside the loop, but throwing away the results, then putting the original string $dna into the @windows list.

Also, the third argument to substr is the number of characters you want; using 0 will always return an empty string. And in your loop, you're starting at 10 and stopping when the position is greater than the number of elements in @dna, but there's only one element in that list, so the loop never executes.

I think something closer to what you mean is:

use constant MOVEMENT => 3; use constant WINDOWSIZE => 10; my $dna = 'accatgagctgtacgtagcatctgagcgcgcatgactgtgactgacgtaggcagca'; my @windows=(); for (my $pos = 0; $pos <= (length($dna) - WINDOWSIZE); $pos += MOVEMEN +T) { push(@windows,substr($dna,$pos,WINDOWSIZE)); } print "@windows\n";

In reply to Re: substr help by sgifford
in thread substr help 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.