in reply to Re: Re: making a single column out of a two-column text file
in thread making a single column out of a two-column text file
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#!/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
|
|---|