Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

fivetoadsloth

macrumors 65816
Original poster
Aug 15, 2006
1,035
0
Hello all,

Sorry for yet another thread, but I'm yet again stuck. I've consulted a perl book that I have and looked on the internet, but didn't find something that I could sucesfully modify.

I wrote two perl pattern matching programs and another program combining them giving me a .txt file in the form:
Code:
A B
C F
G K
L Y
T O
M K
Etc...

I'm guessing I could modify the other programs to change the form of the output, but I'm guessing it's easier to write a nother program to merely reformat.
My goal is to read in the text file and have every x entries of column one be listed, then all the corresponding entries of column two (with a space in the middle) and then a new line.

So:
Code:
ACG BKF
LTM YOK
Etc

If anyone could point me in the right direction that would be great.

Thanks!
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Code:
my $numPerCol = 3;
my $lineNum = 0;
my $colOne;
my $colTwo;
while(<STDIN>) {
  my @parts = split(' ',$_);
  $colOne .= $parts[0];
  $colTwo .= $parts[1];
  $lineNum++;
  if($lineNum == $numPerCol) {
    $lineNum = 0;
    print $colOne . ' ' . $colTwo . "\n";
  }
}

print $colOne . ' ' . $colTwo . "\n" unless($lineNum == 0);

Pipe the output through STDIN and I think this will do it.

-Lee
 

fivetoadsloth

macrumors 65816
Original poster
Aug 15, 2006
1,035
0
Code:
my $numPerCol = 3;
my $lineNum = 0;
my $colOne;
my $colTwo;
while(<STDIN>) {
  my @parts = split(' ',$_);
  $colOne .= $parts[0];
  $colTwo .= $parts[1];
  $lineNum++;
  if($lineNum == $numPerCol) {
    $lineNum = 0;
    print $colOne . ' ' . $colTwo . "\n";
  }
}

print $colOne . ' ' . $colTwo . "\n" unless($lineNum == 0);

Pipe the output through STDIN and I think this will do it.

-Lee


Hey, thanks a lot. One question: I'm reading in a 5.7 mb .txt file, and I'm having to stop the program after a few moments because the output file quickly reaches outrageous sizes (2+ gb). Sicne I'm only reformatting the text, the output should be no bigger then the original file, right? At least, not that much larger? I ran the program for a few seconds and stopped it and looked at the ouput and it looks like as the program continues to run, it is adding 500 more each time. That is, it is just taking the first 500 and adding the next 500, and then the next, so each one is 500 longer then the one before it.

I thought that setting $linNum=0; as you did solved that, but I guess it doesn't. If anyone could help me out, that'd be great. Thanks!
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Hey, thanks a lot. One question: I'm reading in a 5.7 mb .txt file, and I'm having to stop the program after a few moments because the output file quickly reaches outrageous sizes (2+ gb). Sicne I'm only reformatting the text, the output should be no bigger then the original file, right? At least, not that much larger? I ran the program for a few seconds and stopped it and looked at the ouput and it looks like as the program continues to run, it is adding 500 more each time. That is, it is just taking the first 500 and adding the next 500, and then the next, so each one is 500 longer then the one before it.

I thought that setting $linNum=0; as you did solved that, but I guess it doesn't. If anyone could help me out, that'd be great. Thanks!

I screwed this up. Writing late at night on my phone isn't the best. $colOne and $colTwo need to be assigned '' after each print inside the if block. Easy to lose my place when I can only see 8-10 lines at a time.

-Lee
 

fivetoadsloth

macrumors 65816
Original poster
Aug 15, 2006
1,035
0
I screwed this up. Writing late at night on my phone isn't the best. $colOne and $colTwo need to be assigned '' after each print inside the if block. Easy to lose my place when I can only see 8-10 lines at a time.

-Lee

Again, thanks a lot. I'm still a little confused. I have this:

Code:
#!/usr/bin/perl
open(IN, "myfile.txt");
my $Length = 500;
my $lineNum = 0;
my $colOne;
my $colTwo;
while(<IN>) {
  my @parts = split(' ',$_);
  $colOne .= $parts[0];
  $colTwo .= $parts[1];
  $lineNum++;
  if($lineNum == $Length) {
    $lineNum = 0;
        $colOne .= $parts[0];
        $colTwo .= $parts[1];
    print $colOne . ' ' . $colTwo . "\n";
  }
}
print$colOne . ' ' . $colTwo . "\n" unless($lineNum == 0);

But that still isn't giving me the correct output. Sorry!
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Code:
#!/usr/bin/perl
open(IN, "myfile.txt");
my $Length = 500;
my $lineNum = 0;
my $colOne;
my $colTwo;
while(<IN>) {
  my @parts = split(' ',$_);
  $colOne .= $parts[0];
  $colTwo .= $parts[1];
  $lineNum++;
  if($lineNum == $Length) {
    $lineNum = 0;
    print $colOne . ' ' . $colTwo . "\n";
    $colOne = '';
    $colTwo = '';
  }
}
print$colOne . ' ' . $colTwo . "\n" unless($lineNum == 0);

You used 3 input lines to 1 output in your original description, looks like you changed this to 500 input lines to 1 output lines. That's fine, but you'll have some unwieldy 1001 character lines.

-Lee
 

dmi

macrumors regular
Dec 21, 2010
162
33
Code:
perl -lane '$l[1]||=" ";$_.=shift @F for @l; print splice@l,0,2 unless $.%3'
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.