#!C:\perl\bin use strict; #use warnings; # commented out because it throws a warning in the middle #use diagnostics; # of execution, about @polea being uninitialized. ?? # Towers of Hanoi # Perl version (5.10.0) my $numdisks = 0; my $count = 0; my $grandTotal = 0; print "Number of disks? "; chomp( $numdisks = ); clear (); my $i = 0; my @polea = ""; my @poleb = ""; my @polec = ""; my $loop = 0; my $string = ""; while ($numdisks > $loop++) { $string = $string ."x"; push (@polea, $string); } print "A\t\t\tB\t\t\tC\n"; for (my $len = 0 ;$len <= $numdisks; $len++) { print "$polea[$len]\t\t\t$poleb[$len]\t\t\t$polec[$len]\n"; } sleep 1; movedisks( $numdisks, 'A', 'B', 'C' ); print "Total: ", $grandTotal, " \n"; # SUB LAND sub clear { if ($^O eq "MSWin32") { system 'cls'; }else{ system 'clear'; } } sub movedisks { my( $num, $from, $to, $aux ) = @_; if( $num == 1 ) { paintdisks ($num, $to, $from); }else{ movedisks( $num-1, $from, $aux, $to ); paintdisks ($num, $to, $from); movedisks( $num-1, $aux, $to, $from ); } } sub paintdisks { my( $numb, $dest, $source) = @_; my $foo = ""; if ($source eq "A") { $foo = $polea[0]; shift @polea; }elsif ($source eq "B") { $foo = $poleb[0]; shift @poleb; }else{ $foo = $polec[0]; shift @polec; } if ($dest eq "A") { unshift @polea, $foo; }elsif ($dest eq "B") { unshift @poleb, $foo; }else{ unshift @polec, $foo; } print "A\t\t\tB\t\t\tC\n"; for (my $len = 0 ;$len <= ($numdisks -1); $len++) { print "$polea[$len]\t\t\t$poleb[$len]\t\t\t$polec[$len]\n"; } $grandTotal ++; sleep 1; # clear (); commented out so I can see the execution! } #### Results of execution: Number of Disks: 3 A B C x xx xxx A B C x xx xxx A B C xx x xxx A B C xx << here it pushes a space! xxx x A B C xxx xx x A B C << same here xx x xxx A B C x xxx xx A B C xxx << again here x xx Total: 7 # I'm at a loss # # Result that appears when use warnings & use diagnostics are uncommented # A B C Use of uninitialized value in concatenation (.) or string at towers2.pl line 29, line 1 (#1) (W uninitialized) An undefined value was used as if it were already defined. It was interpreted as a "" or a 0, but maybe it was a mistake. To suppress this warning assign a defined value to your variables. To help you figure out what was undefined, perl will try to tell you the name of the variable (if any) that was undefined. In some cases it cannot do this, so it also tells you what operation you used the undefined value in. Note, however, that perl optimizes your program and the operation displayed in the warning may not necessarily appear literally in your program. For example, "that $foo" is usually optimized into "that " . $foo, and the warning will refer to the concatenation (.) operator, even though there is no . in your program. x xx xxx A B C x Use of uninitialized value in concatenation (.) or string at towers2.pl line 79, line 1 (#1) xx xxx A B C xx x xxx Use of uninitialized value within @polea in concatenation (.) or string at towers2.pl line 79, line 1 (#1) A B C xx xxx x A B C xxx xx x A B C xx x xxx A B C x xxx xx A B C xxx x xx Total: 7 #### C:\Perl\working>perl towers2.pl Not enough arguments for map at towers2.pl line 32, near """)" Global symbol "$polea" requires explicit package name at towers2.pl line 32. Global symbol "$poleb" requires explicit package name at towers2.pl line 32. Global symbol "$polec" requires explicit package name at towers2.pl line 32. Global symbol "$polea" requires explicit package name at towers2.pl line 84. Global symbol "$poleb" requires explicit package name at towers2.pl line 84. Global symbol "$polec" requires explicit package name at towers2.pl line 84. Execution of towers2.pl aborted due to compilation errors (#1) (F) The function requires more arguments than you specified. Uncaught exception from user code: Not enough arguments for map at towers2.pl line 32, near """)" Global symbol "$polea" requires explicit package name at towers2.pl line 32. Global symbol "$poleb" requires explicit package name at towers2.pl line 32. Global symbol "$polec" requires explicit package name at towers2.pl line 32. Global symbol "$polea" requires explicit package name at towers2.pl line 84. Global symbol "$poleb" requires explicit package name at towers2.pl line 84. Global symbol "$polec" requires explicit package name at towers2.pl line 84. Execution of towers2.pl aborted due to compilation errors. at towers2.pl line 91 #### my $i = 0; #my @polea; #This was conflicting with the map initialization below my @poleb; my @polec; my $loop = 0; my $string = ""; #while ($numdisks > $loop++) { # $string = $string ."x"; # push (@polea, $string); #} my @polea = map 'x' x $_, 1 .. $numdisks; print "A\t\t\tB\t\t\tC\n"; for (my $len = 0 ;$len <= $numdisks; $len++) { # print "$polea[$len]\t\t\t$poleb[$len]\t\t\t$polec[$len]\n"; 32 print join "\t\t\t", map ($_->[$len] || ""), \$polea, \$poleb, \$polec; print "\n"; } #### print join "\t\t\t", map {[$_]->[$len] || ""} \my $polea, \my $poleb, \my $polec; print "\n"; #### A B C x SCALAR(0x183ce7c) SCALAR(0x183ceac) SCALAR(0x183cecc) xx xxx xxxx xxxxx Total Moves: 31