#!/usr/bin/perl

# ----------------------------------------------------------------
# Interleaves lines of multiple paragraphs.  Believe it or not,
# this comes up sometimes during coding.
#
# John Kerl
# kerl.john.r@gmail.com
# 2005-06-06
# ----------------------------------------------------------------

$printcr = 1;
if (@ARGV && ($ARGV[0] eq "-nocr")) {
	$printcr = 0;
	shift @ARGV;
}

# Read all the input paragraphs at once.
$/="";
my @paragraphs = <>;

# For each paragraph, split into lines.
my $npara = @paragraphs;
$maxrows = 0;

for ($i = 0; $i < $npara; $i++) {
	my @lines = split /\n/, $paragraphs[$i];
	my $nlines = @lines;
	for ($j = 0; $j < $nlines; $j++) {
		my $len = length($lines[$j]);
		$matrix[$i][$j] = $lines[$j];
	}
	if ($nlines > $maxrows) {
		$maxrows = $nlines;
	}
}

# Print the paragraphs side-by-side.
for ($j = 0; $j < $maxrows; $j++) {
	for ($i = 0; $i < $npara; $i++) {
		if ($matrix[$i][$j] ne "") {
			$val = $matrix[$i][$j];
		}
		else {
			$val = "";
		}
		print "$val\n";
	}
	print "\n" if $printcr;
}
