Richard Hobson wrote:
Hi,

Hello,

Please be patient with this beginner. I have a subrouting as follows,
that prints out an ASCII representation of chess board

sub display_board {
    foreach (0..7) {
        my $ref = @_[$_];

That should be:

          my $ref = $_[$_];

Or better:

      foreach my $ref ( @_ ) {


        foreach (0..7) {
            my $piece = $ref->[$_];
            $piece =~ /.*(..$)/;
            print $pieces{$1};

You shouldn't use the numerical variables like $1 unless the match was successful otherwise the value will be left over from the last successful match. The .* at the beginning is superfluous as the pattern is anchored to the end of the string.

You probably want something like:

          foreach my $piece ( @$ref ) {
              print $pieces{ substr $piece, -2 };


        }
        print "\n";
    }
};

It works, but is there a way of combining these lines:

                        my $piece = $ref->[$_];
                        $piece =~ /.*(..$)/;

It feels like this could be done in one step. Is this correct? I'm
finding that I'm doing alright in Perl, but I sense the Perl urge to do
things in as few a number of steps as possible.

sub display_board { print map $pieces{ substr $_, -2 }, map { @$_, "\n" } @_ }




John
--
Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to