#!/usr/bin/perl

use strict; use warnings;

# the var names are for a "rows first" table

my ($rows, $rc) = @ARGV;

$rc or die "usage: $0 n r|c";


my $x = 0;
my $y = 0;

my $dat = [];

while (defined ($_ = <STDIN>)) {
	chomp;
	$dat->[$y][$x] = $_;
	++$y;
	if ($y == $rows) {
		$y = 0; ++$x;
	}
}

my $cols = $x+1;
if ($y == 0) { --$cols; }
if ($cols == 1) { $rows = $y; }

if ($rc eq "c") {
	# transpose
	my $d1 = [];
	for $x (0..$cols-1) {
		for $y (0..$rows-1) {
			$d1->[$x][$y] = $dat->[$y][$x] if defined $dat->[$y][$x];
		}
	}

	$dat = $d1;
	my $t = $rows; $rows = $cols; $cols = $t;
}

for $y (0..$rows-1) {
	print join "\t", @{$dat->[$y]};
	print "\n";
}
