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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.