sub rect { my ($n, $m) = @_; $g = '+' . '-' x $m . '+'; my @a = ($g) x 2; $g = '|' . ' ' x $m . '|'; @a = (@a, ($g) x $n)[1 .. $n+1, 0]; return @a; } sub divide { my ($n, $m, @a) = @_; my $le; $le = length for @a; my $w = ($le - 2) / $m; %h = ('-' => '+', ' ' => '|'); s/([- ]{$w}) (?! [+|])/ $1 . join ' ', ( $h{ chop (my $i = $1) } ) x 2 /gxe for @a; my $jump = (@a - 2) / $n; my @c; (push @c, $_), !$_ || $_ % $jump or push @c, (0) x 2 for 0 .. @a-2; pop @c; return @a[@c] } $, = $/; @q = qw( 4 6 2 3 3 4 3 2 5 2 1 2 2 8 2 1 6 3 1 1 4 7 2 7 1 2 1 2 4 9 2 3 8 12 4 6 7 14 1 7 9 9 3 9 5 22 1 11 ); while(@q){ # print @_ = rect (shift @q, shift @q), ''; my ($n, $m) = (shift @q, shift @q); # print $n . ' ' . $m . $/; # print divide ($n, $m, @_), ''; } my @a = (); while(<>){ chomp; !/\d/ ? (push @a, $_) : do { my ($n, $m) = split; print divide ($n, $m, @a), ''; @a = (); }; }