http://qs321.pair.com?node_id=648942

convenientstore has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,
I think I need some help understanding Hanoi tower program. I was just going over Hanoi in subroutine tutorial section and I can't exactly follow the program.
How does below step get --> 3 ??

I think I understand the algorithm but I don't think I understand the technique this program is using

----Move disk #3 from A to C.----

#!/usr/bin/perl -w use strict; sub hanoi { my ($n, $start, $end, $extra) = @_; if ($n == 1) { print "HELLLLLOOOOOOOOOOOOO Move disk #1 from $start to $end.\n +"; } else { hanoi($n-1, $start, $extra, $end); print "Move disk #$n from $start to $end.\n"; hanoi($n-1, $extra, $end, $start); } } hanoi(3, 'A', 'C', 'B'); ./perl.hanoi1 HELLLLLOOOOOOOOOOOOO Move disk #1 from A to C. Move disk #2 from A to B. HELLLLLOOOOOOOOOOOOO Move disk #1 from C to B. Move disk #3 from A to C. HELLLLLOOOOOOOOOOOOO Move disk #1 from B to A. Move disk #2 from B to C. HELLLLLOOOOOOOOOOOOO Move disk #1 from A to C. DB<2> main::hanoi(perl.hanoi1:7): if ($n == 1) { + DB<2> +x $n 0 1 + DB<3> +s main::hanoi(perl.hanoi1:8): print "HELLLLLOOOOOOOOOOOOO Mov +e disk #1 from $start to $end.\n"; + DB<3> +s HELLLLLOOOOOOOOOOOOO Move disk #1 from A to C. main::hanoi(perl.hanoi1:11): print "Move disk #$n from $sta +rt to $end.\n"; + DB<3> +s Move disk #2 from A to B. main::hanoi(perl.hanoi1:12): hanoi($n-1, $extra, $end, $sta +rt); + DB<3> +x $n 0 2 + DB<4> +s main::hanoi(perl.hanoi1:6): my ($n, $start, $end, $extra) = @_; + DB<4> +x $n 0 undef + DB<5> +s main::hanoi(perl.hanoi1:7): if ($n == 1) { + DB<5> +x $n 0 1 + DB<6> +s main::hanoi(perl.hanoi1:8): print "HELLLLLOOOOOOOOOOOOO Mov +e disk #1 from $start to $end.\n"; + DB<6> +s HELLLLLOOOOOOOOOOOOO Move disk #1 from C to B. main::hanoi(perl.hanoi1:11): print "Move disk #$n from $sta +rt to $end.\n"; + DB<6> +x $n 0 3