in reply to making a single column out of a two-column text file

use strict; my $pattern = "A29 A*"; my (@lefthand,@righthand); while ( <DATA> ) { chomp; my ($lhs,$rhs) = unpack ($pattern,$_); push (@lefthand,$lhs); push (@righthand,$rhs); } print "\nLEFTHAND STUFF:\n"; print join "\n", @lefthand; print "\nRIGHTHAND STUFF:\n"; print join "\n", @righthand; __DATA__ LHC RHC By deleting the initial You have removed the whitespace you are going whitespace that was to move what was in the in front of the left right hand column all hand column. This the way to the left. approach might be better than what you were trying. This is because If nothing else it is a new direction.

-enlil

Replies are listed 'Best First'.
Re: Re: making a single column out of a two-column text file
by allolex (Curate) on Feb 26, 2003 at 03:22 UTC

    Well, thanks! This one works well, but the disadvantage is you have to provide a column offset as a constant. I was hoping to find something that found the gap by itself, but you were definitely right about me needing to think in different ways and try a different approach. And this is a very practical approach, quite in the spirit of what I am learning Perl for.

    --
    Allolex

      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