in reply to Re^3: How do foreach loops use their list values?
in thread [Solved]How do foreach loops use their list values?

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.

Replies are listed 'Best First'.
Re^5: How do foreach loops use their list values?
by Athanasius (Archbishop) on Nov 03, 2013 at 03:31 UTC

    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:

    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,

      Oh wow thank you so much! I did not realise @words was not being reinitialised after each iteration through the loop (which makes sense, since it was declared outside the loop).

      Thanks a lot, all of you, I think I learnt a lot by trying to understand this code. Have a nice Sunday.

        If you're interested in permutations and genomic sequences and in advanced Perl techniques, you might want to grab a copy of Dominus's Higher Order Perl (HOP), with reference in particular to sections 4.3.1 "Permutations" and 4.3.2 "Genomic Sequence Generator". The book and its errata are freely downloadable here and well worth the bandwidth even if you're on dial-up. The material is a bit advanced, but if you're interested in a whiff of "air from another world", grab this, keep it around and dip into it from time to time.

        Another good freely downloadable book, one more designed for the sophisticated beginner in Perl (and one more in tune with current practice; HOP is perhaps just a bit dated in this respect) is chromatic's Modern Perl; see chromatic's user node for download links.

        Update: I just noticed that [Solved][HOP] Working with Iterators, why do I suddenly get a coderef? is by someone working with HOP; you might be interested.