in reply to Re: Fibonacci numbers(Again)
in thread Fibonacci numbers

How does this statement
($a,$b)=($a,$a+$b);
does the trick? I couldn't understand...can somebody explain?

Replies are listed 'Best First'.
Re: Re: Re: Fibonacci numbers(Again)
by pixel (Scribe) on Oct 03, 2001 at 14:02 UTC

    Perhaps you can't see it because you've got the statement wrong. It should be

    ($a,$b)=($b,$a+$b);

    It's a simple list assignment. $a gets the value of $b and $b gets $a+$b.

    Incidently, it can be a bad idea to use $a and $b in Perl scripts as they are "special" variables because of their use in sorting.

    Blessed Be
    The Pixel

Re: Re: Re: Fibonacci numbers(Again)
by saiful (Novice) on Oct 03, 2001 at 13:45 UTC
    Dear Anonymous, It's very simple. In every iteration the $a receives the earlier value of $b...that's the trick. Bye the way, u can see my ealier attempt, which had one serious mistake... that can be rewritten to give the desired result. See:
    $a=1; $b=1; while($a <=200) {$c=$a+$b; print("$a\n$b\n$c\n"); $a=$b+$c; $b=$c+$a; }
    Thanks.
Re: Re: Re: Fibonacci numbers(Again)
by tommyw (Hermit) on Oct 03, 2001 at 02:49 UTC

    Oh, for goodness sake. This is homework, right?

    For the answer to this latest question, see the first answer in the thread...

    (Alternatively, read what you're quoting: it might make more sense then.)