my %DISPATCH_TABLE; BEGIN { %DISPATCH_TABLE = ( n => sub { my ($oriented, $matrix) = @_; push $oriented->@*, $matrix->[$_] for reverse 0 .. $matrix->$#*; }, s => sub { my ($oriented, $matrix) = @_; push $oriented->@*, $matrix->[$_] for 0 .. $matrix->$#*; }, e => sub { my ($oriented, $matrix) = @_; for my $x (0 .. $matrix->$#*) { push $oriented->@*, my $col = []; for my $y (0 .. $matrix->[$x]->$#*) { push $col->@*, $matrix->[$y][$x]; } } }, w => sub { my ($oriented, $matrix) = @_; for my $x (0 .. $matrix->$#*) { push $oriented->@*, my $col = []; for my $y (reverse 0 .. $matrix->[$x]->$#*) { push $col->@*, $matrix->[$y][$x]; } } }, ); } sub orient_matrix { my ($orient, $matrix) = @_; my $code = $DISPATCH_TABLE{$orient} or die "Unknown orientation '$orient'"; $code->(my $oriented = [], $matrix); return $oriented; }