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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |