Hello, I am fairly new to Perl and Perlmonks so I hope I'm not breaking any rules by posting this message here! I was having a look at some algorithms on Bioperl (http://www.bioperl.org/wiki/Getting_all_k-mer_combinations_of_residues). Basically, out of the four letters 'A', 'T', 'G' and 'C', the following code gives me all the combinations of k-long "words" (so if k = 3, it would produce AAA, AAT, AAG... CCC):

use warnings; use strict; sub kmers { my $k = shift; my $alphabet = qw( A T G C ); my @bases = @$alphabet; my @words = @bases; for ( 1 .. --$k ) { my @newwords; foreach my $w (@words) { foreach my $b (@bases) { push (@newwords, $w.$b); } } @words = @newwords; } return @words; }

I can understand most of this code, but I have trouble understanding how the

for (1 .. --$k)

loop is being implemented. Whenever I've seen these for/foreach loops, once INSIDE the loop, you usually do something with the list of numbers inside the parenthesis (in this case, numbers 1 through 'k minus one'). In this case, I cannot see how the list is being used. I have read the Perldoc entry on foreach loops, but I couldn't find out how this loop works.

Could someone please refer me to a resource/reference that explains how such a loop might be working? Again, I'm sorry if this seems to be too trivial compared to most of the questions asked on this site.


In reply to [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.