Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-25 21:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found