Working on a 'chessboard' type of script. As you can guess, this is a row/column matrix. Below is the code and subsequent errors:
my @chessboard;
my @back = qw(R N B Q K B N R);
foreach (0..7) {
$chessboard[0][$_] = "W" . $back[$_}; # White Back Row
$chessboard[1][$_] = "WP"; # White Pawns
$chessboard[6][$_] = "BP"; # Black Pawns
$chessboard[7][$_] = "B" . $back[$_]; # Black Back Row
}
while (1) {
# Print board
foreach my $i (reverse (0..7)) { # Row
foreach my $j (0..7) { # Column
if (defined $chessboard[$i][$j]) {
print $chessboard[$i]$j];
} elseif ( ($i % 2) == ($j %2) ) {
print "..";
} else {
print " ";
}
print " "; # End of cell
} # end of foreach
print "\n"; # End of row
print "\nStarting square [x,y]: ";
my $move = <>;
last unless ($move =~ /^\s([1-8]),([1-8)/);
my $startx = $1-1; my $starty = $2-1;
unless (defined $chessboard[$starty][$startx]) {
print "There's nothing on that square!\n";
next;
}
print "\nEnding square [x,y]: ";
$move = <>;
last unless ($move =~ /([1-8]},([1-8])/);
my $endx = $1-1; my $endy = $2-1;
# Put starting square on ending square
$chessboard[$endy][$endx] = $chessboard[$starty][$startx];
# remove from old square
undef $chessboard[$starty][$startx];
}
"my" variable @chessboard masks earlier declaration in same scope at .
+/BP_Chap11_Exer1.pl line 29.
"my" variable $i masks earlier declaration in same statement at ./BP_C
+hap11_Exer1.pl line 29.
"my" variable $j masks earlier declaration in same statement at ./BP_C
+hap11_Exer1.pl line 29.
"my" variable @chessboard masks earlier declaration in same statement
+at ./BP_Chap11_Exer1.pl line 30.
"my" variable $i masks earlier declaration in same statement at ./BP_C
+hap11_Exer1.pl line 30.
Scalar found where operator expected at ./BP_Chap11_Exer1.pl line 30,
+near "]$j"
(Missing operator before $j?)
elseif should be elsif at ./BP_Chap11_Exer1.pl line 31.
syntax error at ./BP_Chap11_Exer1.pl line 19, near "$_}"
syntax error at ./BP_Chap11_Exer1.pl line 23, near "}"
Global symbol "$j" requires explicit package name at ./BP_Chap11_Exer1
+.pl line 30.
Global symbol "$i" requires explicit package name at ./BP_Chap11_Exer1
+.pl line 31.
Global symbol "$j" requires explicit package name at ./BP_Chap11_Exer1
+.pl line 31.
Unmatched [ in regex; marked by <-- HERE in m/^\s([1-8]),([ <-- HERE 1
+-8)/ at ./BP_Chap11_Exer1.pl line 42.
TIA
The Catfish
-
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.
|