Dear Brethren,

I have been working on a small script that is supposed to take a text that has been formatted into two columns (with spaces in between the columns) and convert this text into single-column format.

So far, the script does what it is supposed to do... mostly...(ahem) What it does not do properly is handle non-existent left-hand columns--ones that are just spaces. When it processes those lines, the actual right-hand text lands in my @left array.

So, can someone point me in the right direction?

Oh, yes. And a bit of conceptual help...no, no smoove music.

LHC RHC 111111111111 222222222222 444444444444 555555555555 666666666666 777777777777 888888888888

In other words, it puts "4" into the slot for "3" (which is a bunch of spaces). I tried prefiltering with a substitution regexp, something like s/(\s{31,})/x$1x/;, but it didn't work.

'Nuff said

#!/usr/bin/perl use strict; use warnings; my $input = shift; my @first; my @second; my $counter = 0; if (!$input) {die "\nUsage: $0 inputfile\n"}; print STDERR "\nConverting file $input.\n"; open (INFILE, "< $input") or die "\nThe input file cannot be opened: \ +!\n"; while ( <INFILE> ) { chomp; s/^\s+//g; # get rid of leading spaces s/\n//g; ($first[$counter],$second[$counter]) = split(/[ ]{3,30}/,$_); # pr +etty much arbitrarily defined min and max $counter++; } close(INFILE); print "First column output:\n\n@first\n\n"; print "Second column output:\n\n@second\n\n";

Any other suggestions for the improvment of my code are welcome, too.

--
Allolex


In reply to making a single column out of a two-column text file by allolex

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.