write all lines except the first one!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tuananh87vn
    New Member
    • Sep 2007
    • 63

    write all lines except the first one!

    i'm about to use perl to read the content of a text file then write all lines in this file, except the first line, into another text file. Reading and writing file I think is ok to me, but I don't know how to make the first line as exception :|

    any help?
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    If you know how to write all lines in a loop, just read from the file once before entering the loop. Essentially, you need to get the first line, then enter into a loop that prints everything (remaining) in the file.

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      That's what I love about Perl..... TMTOWTDI !!!

      Code:
      my $counter = 0;
      
      open(FILE, "<file.txt") or die "$!";
      
      LOOP: while(<FILE>) {
          if($counter = 0){
              $counter++;
              next LOOP;
          }
      
          .... the rest of your code...
      }
      Just another way of doing it using a counter. The first time through it will read a line, check the counter, which is equal to zero, iterate the counter and then grab the next line and continue processing.

      Regards,

      Jeff

      Comment

      • tuananh87vn
        New Member
        • Sep 2007
        • 63

        #4
        my bad,
        just when I put the question on the forum, the answer rushed into my head:

        Code:
        open <FILE, 'text1'>;
        open <FILE2, 'text2'>;
        
        $i = 0;
        
        while (<FILE>) {
           if ($i != 0) {
               print FILE2 "$_\n";
           }
           $i++;
        }
        
        close (FILE1);
        close(FILE2);

        do u think this way is ok?

        thanx so much for your help :D

        Comment

        • numberwhun
          Recognized Expert Moderator Specialist
          • May 2007
          • 3467

          #5
          Originally posted by tuananh87vn
          my bad,
          just when I put the question on the forum, the answer rushed into my head:

          Code:
          open <FILE, 'text1'>;
          open <FILE2, 'text2'>;
          
          $i = 0;
          
          while (<FILE>) {
             if ($i != 0) {
                 print FILE2 "$_\n";
             }
             $i++;
          }
          
          close (FILE1);
          close(FILE2);
          do u think this way is ok?

          thanx so much for your help :D
          It does look to pretty much do the same thing, yes. Nice job. All in all though, remember, Perl's motto has always been..... TMTOWTDI !!!

          Nice job!

          Regards,

          Jeff

          Comment

          • tuananh87vn
            New Member
            • Sep 2007
            • 63

            #6
            a mistake, it should be
            Code:
            open(FILE2, '>text2')
            so that we can write on file text2

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              This is much more efficient:


              Code:
              open(IN, "<file.txt") or die "$!";
              open (OUT, ">", 'outfile.txt') or die "$!";
              <IN>; #<-- discard first line
              print OUT <IN>;
              close(IN);
              close(OUT);

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                Originally posted by numberwhun
                That's what I love about Perl..... TMTOWTDI !!!

                Code:
                my $counter = 0;
                
                open(FILE, "<file.txt") or die "$!";
                
                LOOP: while(<FILE>) {
                
                
                        $counter++;
                        next LOOP;
                    }
                
                    .... the rest of your code...
                }
                Just another way of doing it using a counter. The first time through it will read a line, check the counter, which is equal to zero, iterate the counter and then grab the next line and continue processing.

                Regards,

                Jeff

                This line is wrong:

                Code:
                    if($counter = 0){
                I think you can see why Jeff.

                Comment

                • numberwhun
                  Recognized Expert Moderator Specialist
                  • May 2007
                  • 3467

                  #9
                  Originally posted by KevinADC
                  This line is wrong:

                  Code:
                      if($counter = 0){
                  I think you can see why Jeff.

                  He he, my quick, on the fly, untested code. Yes, it should be ==, sorry. :)

                  Comment

                  Working...