PDA

View Full Version : (Another) Perl Question




fivetoadsloth
Apr 1, 2011, 11:34 PM
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:

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:

ACG BKF
LTM YOK
Etc

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

Thanks!



lee1210
Apr 2, 2011, 12:21 AM
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
Apr 2, 2011, 01:44 PM
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
Apr 2, 2011, 02:55 PM
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
Apr 2, 2011, 03:53 PM
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:

#!/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
Apr 2, 2011, 04:28 PM
#!/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
Apr 2, 2011, 08:15 PM
perl -lane '$l[1]||=" ";$_.=shift @F for @l; print splice@l,0,2 unless $.%3'

mfram
Apr 2, 2011, 09:25 PM
.