Huge thank you to everyone as I finally got it..
It took re-reading everyone's post and trying combination of different things.. but I finally got it. Thank you so much!!!!
Long time ago, I took a pascal course and there was a tracing program where I can actually step through the program and I personally think perl's debugging mechanism is little bit behind.(but then what do i know)
I ran the debug on this program but I was not able to follow through.. now that i am running with extra print and indentation, I can clearly see
btw, Mark, when is book "Perl Program Repair Shop and Red Flags" coming out? I am reading the "higher learning" (but bit too advance for me at this stage) and I feel that beginners like me can benefit big time by reading the completed book of reapir shop
1 #!/usr/bin/perl -w
2 use strict;
3
4 my $recursion = 0;
5 my $count;
6
7 sub hanoi {
8 warn "invoked @_";
9 my ($n, $start, $end, $extra) = @_;
10 $recursion++;
11 my $indent = " " x $recursion;
12
13 print$indent, "Entering hanoi($n, $start, $end, $extra)\n";
14
15 if ($n == 1) {
16 warn $indent, "----------Move disk #1 from $start to $e
+nd.------------\n";
17 $count++;
18 } else {
19 print"enter first hanoi\n";
20 print"\$n is $n\n";
21 hanoi($n-1, $start, $extra, $end);
22 print"AFTER first hanoi\n";
23 print "\$n is $n\n";
24 warn $indent, "----------$count Move disk #$n from $st
+art to $end.\n";
25 print " enter second hanoi\n";
26 hanoi($n-1, $extra, $end, $start);
27 print " AFTER second hanoi\n";
28 print " \$n is $n\n";
29 }
30 print $indent, " __LEAVING hanoi($n, $start, $end,
+ $extra)\n";
31 print " \$n is $n\n";
32 $recursion--;
33 }
34
35 hanoi(3, 'A', 'C', 'B');
invoked 3 A C B at ././perl.hanoi_f line 8.
Entering hanoi(3, A, C, B)
enter first hanoi
$n is 3
invoked 2 A B C at ././perl.hanoi_f line 8.
Entering hanoi(2, A, B, C)
enter first hanoi
$n is 2
invoked 1 A C B at ././perl.hanoi_f line 8.
Entering hanoi(1, A, C, B)
----------Move disk #1 from A to C.------------
__LEAVING hanoi(1, A, C, B)
$n is 1
AFTER first hanoi
$n is 2
----------1 Move disk #2 from A to B.
enter second hanoi
invoked 1 C B A at ././perl.hanoi_f line 8.
Entering hanoi(1, C, B, A)
----------Move disk #1 from C to B.------------
__LEAVING hanoi(1, C, B, A)
$n is 1
AFTER second hanoi
$n is 2
__LEAVING hanoi(2, A, B, C)
$n is 2
AFTER first hanoi
$n is 3
----------2 Move disk #3 from A to C.
enter second hanoi
invoked 2 B C A at ././perl.hanoi_f line 8.
Entering hanoi(2, B, C, A)
enter first hanoi
$n is 2
invoked 1 B A C at ././perl.hanoi_f line 8.
Entering hanoi(1, B, A, C)
----------Move disk #1 from B to A.------------
__LEAVING hanoi(1, B, A, C)
$n is 1
AFTER first hanoi
$n is 2
----------3 Move disk #2 from B to C.
enter second hanoi
invoked 1 A C B at ././perl.hanoi_f line 8.
Entering hanoi(1, A, C, B)
----------Move disk #1 from A to C.------------
__LEAVING hanoi(1, A, C, B)
$n is 1
AFTER second hanoi
$n is 2
__LEAVING hanoi(2, B, C, A)
$n is 2
AFTER second hanoi
$n is 3
__LEAVING hanoi(3, A, C, B)
$n is 3
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.