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


In reply to ChessBoard Program by catfish1116

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.