Hello enderk, and welcome to the Monastery!
What you’re overlooking is the line:
@words = @newwords;
which updates the contents of @words at the end of each loop iteration. Here’s one way of visualising what is happening:
use warnings; use strict; my @words = kmers(3); sub kmers { my $k = shift; my @bases = qw( A T G C ); my @words = @bases; for my $i (1 .. $k - 1) { print "\nBEGIN \$i = $i:\n"; print " \@bases = @bases\n"; print " \@words = @words\n"; my @newwords; for my $w (@words) { push @newwords, $w.$_ for @bases; } @words = @newwords; print "END \$i = $i:\n"; print " \@bases = @bases\n"; print " \@words = @words\n"; } return @words; }
Output:
13:20 >perl 769_SoPW.pl BEGIN $i = 1: @bases = A T G C @words = A T G C END $i = 1: @bases = A T G C @words = AA AT AG AC TA TT TG TC GA GT GG GC CA CT CG CC BEGIN $i = 2: @bases = A T G C @words = AA AT AG AC TA TT TG TC GA GT GG GC CA CT CG CC END $i = 2: @bases = A T G C @words = AAA AAT AAG AAC ATA ATT ATG ATC AGA AGT AGG AGC ACA ACT ACG + ACC TAA TAT TAG TAC TTA TTT TTG TTC TGA TGT TGG TGC TCA TCT TCG TCC +GAA GAT GAG GAC GTA GTT GTG GTC GGA GGT GGG GGC GCA GCT GCG GCC CAA C +AT CAG CAC CTA CTT CTG CTC CGA CGT CGG CGC CCA CCT CCG CCC 13:20 >
As you can see, on the second iteration ($i == 2), each element of @words is initially two characters in length; so, naturally, when it is concatenated with a base character, the result is three characters long, as required.
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
In reply to Re^5: How do foreach loops use their list values?
by Athanasius
in thread [Solved]How do foreach loops use their list values?
by enderk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |