(Another) Perl Question

Discussion in 'Mac Programming' started by fivetoadsloth, Apr 1, 2011.

  1. macrumors 65816

    fivetoadsloth

    Joined:
    Aug 15, 2006
    #1
    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!
     
  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    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
     
  3. thread starter macrumors 65816

    fivetoadsloth

    Joined:
    Aug 15, 2006
    #3

    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!
     
  4. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    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
     
  5. thread starter macrumors 65816

    fivetoadsloth

    Joined:
    Aug 15, 2006
    #5
    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!
     
  6. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    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
     
  7. dmi
    macrumors regular

    Joined:
    Dec 21, 2010
    #7
    Code:
    perl -lane '$l[1]||=" ";$_.=shift @F for @l; print splice@l,0,2 unless $.%3' 
    
     
  8. macrumors 6502a

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #8
    .
     

Share This Page