use strict; use warnings; my (@grid, $top, $bottom, $left, $right); while () { chomp; push @grid, [split / /]; } $top = 0; $bottom = $#grid; $left = 0; $right = $#{$grid[0]}; while (1) { print $_ for @{$grid[$top]}[$left..$right]; last if ++$top > $bottom; print $grid[$_][$right] for $top..$bottom; last if --$right < $left; print $_ for @{$grid[$bottom]}[reverse $left..$right]; last if --$bottom < $top; print $grid[$_][$left] for reverse $top..$bottom; last if ++$left > $right; } __DATA__ a b c d e f g h i j k l m n o p q r s t u