Richard Hobson <m...@richardhobson.com> wrote:
> 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 = @_[$_];
>               foreach (0..7) {
>                       my $piece = $ref->[$_];
>                       $piece =~ /.*(..$)/;
>                       print $pieces{$1};
>               }
>               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.

How about (untested):

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

The Perl way is to avoid using indices where you don't need them.

HTH,
Thomas

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