This solution fixes the above problem. It actually should work for pretty much any input so long as it is close to natural English. To be more specific, it will work where the start of the right-hand column is the offset into each line of text that is most likely to have a space in front of it. I tried it out on some arbitrary input. I can't guarantee anything, but it seems likely to work in most cases. If there were more than two columns, it would mess things up. It requires two passes through the text, one to figure out the column index of the right-hand column, the second to split the text into two arrays.
#!/usr/bin/perl -w
use strict;
my %colcnt;
my (@text, @lhs, @rhs);
my $max;
my $maxcnt = 0;
while (<DATA>) {
chomp;
push @text, $_; # save text for later
my @chars = split //, $_;
for (my $i = 1; $i < @chars; ++$i) { #skip first char (no pred)
$colcnt{$i}++ if $chars[$i] ne ' ' && $chars[$i - 1] eq ' ';
$colcnt{$i} |= 0; # make sure it is init for warnings
$max = $i and $maxcnt = $colcnt{$i} if $colcnt{$i} > $maxcnt;
}
}
foreach (@text) {
my ($lhs, $rhs) = unpack("A$max A*", $_);
push @lhs, $lhs;
push @rhs, $rhs;
}
print "LHS:\n";
print join "\n", @lhs;
print "\n\nRHS:\n";
print join "\n", @rhs;
__DATA__
This script handles about how
arbitrary spacing much
for column skip as white space is
long as there are in each column. I
enough lines of text can't figure out
and a "normal" dist
of spaces.
how to
It also doesn't make do it in one pass though.
any assumpions
Update: As a result of jdporter's approach below, I was thinking that you could make this better by looking for more than one space before the column start (not just $i - 1). If you can set a definite minimum width on the size of the column gap you will improve the probability of this script working | [reply] [d/l] |