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


In reply to cannot follow hanoi subroutine by convenientstore

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.