You're absolutely right. I copied-pasted the code from bioperl.org onto this post without checking to see if it was wrong. When I used it in my terminal, I typed it all out and automatically changed the $ sign in $alphabet to an @, which is why it worked fine for me. And I agree, I should have gone for @bases directly instead of using that extra array which is not used anywhere else in the code. I will pay more attention to these details next time I code something up, so thanks for the tip!

On another note, I'd like to go back to my original question. I now understand that this foreach loop is being used to repeat its code (k-1) times. Let's assume $k = 3.

When $_ = 1 (first iteration of the foreach loop), we make the strings (AA, AT, AG... CC) and push them all into @newwords.

When $_ = 2, if we repeated the code inside the loop, we should produce another set of (AA...CC) and push them into @newwords, which is reinitialised in this second iteration through the loop.

I know this is not the case, however, because if I add the following code:

print "@newwords\n"

after this line:

push (@newwords, $w.$b);

we can see that we are now pushing (AAA...CCC) to the @newwords array, despite the code being exactly the same as in the first iteration.
This is exactly what I want my code to do, although I don't understand why it is working. We are pushing 3-character strings, although according to this code, we are only pushing strings of two characters ($w.$b) into the @newwords array.


In reply to Re^4: How do foreach loops use their list values? by enderk
in thread [Solved]How do foreach loops use their list values? by enderk

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.