From 6fce46c86da21aa2e28fa353bc1c85c89cb9ee1c Mon Sep 17 00:00:00 2001 From: drbaggy Date: Tue, 23 Feb 2021 05:04:19 +0000 Subject: tidied up layout --- challenge-101/james-smith/perl/ch-1.pl | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/challenge-101/james-smith/perl/ch-1.pl b/challenge-101/james-smith/perl/ch-1.pl index ac89a45459..18434b2b69 100644 --- a/challenge-101/james-smith/perl/ch-1.pl +++ b/challenge-101/james-smith/perl/ch-1.pl @@ -9,32 +9,34 @@ use Test::More; print_spiral( 1 .. $_ ) foreach 1..30; sub print_spiral { - say join q( ), map { sprintf '%2d', $_ } @{$_} foreach @{pack_spiral(@_)}; + say join q( ), map { sprintf '%2d', $_ } @{$_} + foreach @{pack_spiral(@_)}; say ''; } sub pack_spiral { - my( $rows, @in ) = ( 1, @_ ); + my $rows = 1; ## Get the value for columns & rows which have the smallest gap ## but still multiply to size of array (we choose rows to be ## no greater than columns as printing is neater - but for no ## other reason... - $rows = @in%$_ ? $rows : $_ foreach 2 .. sqrt @in; + $rows = @_%$_ ? $rows : $_ foreach 2 .. sqrt @_; - my ($cols,$r,$c,@out) = (@in/$rows,$rows-1,-1); + my ($cols,$r,$c,@out) = (@_/$rows,$rows-1,-1); ## We start bottom left... ## because we use pre-inc we actually start 1 to the left of it! ## as we "jump" before depositing the entry of the array... - - while( $rows && $cols ) { ## Do we have anything else to do? - $out[$r][++$c] = shift @in foreach 1..$cols--; # >> - $out[--$r][$c] = shift @in foreach 1..--$rows; # ^^ - last unless $rows && $cols; ## Skip if we have finished here - $out[$r][--$c] = shift @in foreach 1..$cols--; # << - $out[++$r][$c] = shift @in foreach 1..--$rows; # vv + ## Remember shift by default operates on @_; + + while( @_ ) { # do until empty + $out[ $r ][ ++$c ] = shift foreach 1 .. $cols--; # >> + $out[ --$r ][ $c ] = shift foreach 1 .. --$rows; # ^^ + last unless @_; # exit if empty + $out[ $r ][ --$c ] = shift foreach 1 .. $cols--; # << + $out[ ++$r ][ $c ] = shift foreach 1 .. --$rows; # vv } return \@out; -- cgit