Any perl programmers here? have a question.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RayVega
    Confirmed User
    • Jul 2004
    • 4212

    #1

    Any perl programmers here? have a question.

    Don't ask why...long story.

    Need to insert a line into an array to be written to file (or more accurately a file containing a list)

    So, example, I have a file containing:
    a
    b
    c
    d
    e
    f

    I need to insert z on the 4th line ex:
    a
    b
    c
    d
    z
    e
    f


    Any ideas?
    Ray "The Don" Vega

    Managing Director
    Private Equity Fund

    [email protected]
  • Tempest
    Too lazy to set a custom title
    • May 2004
    • 10217

    #2
    ideas?? or do you want someone to write it for you.

    Comment

    • donborno
      Confirmed User
      • Jan 2007
      • 374

      #3
      Code:
      #!/usr/bin/perl -w
      use strict;
      
      my $INPUT = 'blabla.txt';
      my $OUTPUT = 'blabla2.txt';
      my $FOURTH_LINE = 'z';
      
      open(my $fhi, $INPUT) or die "Cannot open $INPUT : $!";
      open(my $fho, '>', $OUTPUT) or die "Cannot open $OUTPUT (writing): $!";
      
      while (defined(my $line = <$fhi>)) {
          print $fho $line;
          print $fho $FOURTH_LINE, "\n" if ($. == 4);
      }
      
      close($fhi);
      close($fho) or die "Cannot close $OUTPUT : $!";
      But then again, it's 7 am.
      Last edited by donborno; 09-13-2007, 09:16 PM.

      Comment

      Working...