Problems? Is your data what you think it is? PerlMonks

### ChessBoard Program

 on Feb 10, 2020 at 22:02 UTC Need Help??

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

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

Replies are listed 'Best First'.
Re: ChessBoard Program
by Corion (Pope) on Feb 10, 2020 at 22:08 UTC

There are a lot of warnings, and there is one first syntax error at:

```Scalar found where operator expected at ./BP_Chap11_Exer1.pl line 30,
+near "]\$j"

Searching for that string points out this line:

```              print \$chessboard[\$i]\$j];

The next syntax error is

```syntax error at ./BP_Chap11_Exer1.pl line 19, near "\$_}"

Searching for that string points to this line:

```     \$chessboard[0][\$_] = "W" . \$back[\$_};  # White Back Row

The line numbers don't match up with your code. Are you sure that you show us the code that produces these error messages?!

Re: ChessBoard Program
by GrandFather (Sage) on Feb 10, 2020 at 22:45 UTC

Always start with the earliest error in the code. In this case it is on line 19: 'syntax error at ./BP_Chap11_Exer1.pl line 19, near "\$_}"'. Once you fix that issue there are three or four more of a similar nature. Fix those and the elsif issue and you are left with one final issue due to a missing }. Your indentation is mostly pretty good so it should be easy enough to see where the missing } should go.

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re: ChessBoard Program
by LanX (Archbishop) on Feb 10, 2020 at 23:27 UTC
Hello catfish1116

The history of your posts always contain a lot of trivial syntax errors.

Errors of a kind one would easily spot if the code was developed step by step.

Could it be that you are manually copying the code as a whole from a printed text and ask us to spot the typos?

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

It is true that I am manually copying the code from a printed text. Early on, it was my own fault for not typing correctly, (those were earlier posts). My more current posts are a result of me typing exactly how the script appears in the books that I am learning from and the code not working, or if the exercises are asking to do the just one function in the program and I try to make it more robust by adding more functionality. I appreciate being part of the Perlmonk community as I find everyone on this sight extremely helpful and has helped me immensely. Thanks, Catfish

Syntax errors (almost) always force an abort of compilation.

Always test your code with

perl -c your_file.pl

after typing a new line and you'll catch most typos.

Or try the Komodo IDE it'll run the compilation automatically in the background and highlight broken code.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Create A New User
Node Status?
node history
Node Type: perlquestion [id://11112746]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2020-06-01 16:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Do you really want to know if there is extraterrestrial life?

Results (5 votes). Check out past polls.

Notices?