in reply to Re: Genetic Code
in thread Genetic Code

Sorry my ignorance... But I REALLY did not understand this part of the code:

 my $fmt = $FMT_DNA[$i++ % @FMT_DNA];

I searched in books on "%" and nothing there helped me to understand its functioning here...

Please... Give me your wisdom Saint Monk...

Braz Monk

Replies are listed 'Best First'.
Re3: Genetic Code
by dragonchild (Archbishop) on Aug 18, 2003 at 20:24 UTC
    %: Modolus operator. If $a % $b, returns the remainder when $a is divided by $b. Thus, 45 % 6 is 3, as is 51 % 6.

    The code in question does this:

    my $fmt = $FMT_DNA[$i++ % @FMT_DNA];
    1. Get the remainder when $i is divided by the number of elements in @FMT_DNA and put it in a temp variable.
    2. Increment $i by 1.
    3. Get the element in @FMT_DNA with an index of the temp variable and put it in $fmt.

    What this is doing is treating @FMT_DNA as a circular list - a list that will wrap around to the beginning when it runs out of elements. So, if it was 3 elements, it would do element 0, element 1, element 2, element 0, element 1, etc.

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.