467,146 Members | 1,003 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,146 developers. It's quick & easy.

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?
Sep 24 '08 #1
  • viewed: 2319
Share:
8 Replies
Ganon11
Expert 2GB
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.
Sep 24 '08 #2
numberwhun
Expert Mod 2GB
That's what I love about Perl..... TMTOWTDI !!!

Expand|Select|Wrap|Line Numbers
  1. my $counter = 0;
  2.  
  3. open(FILE, "<file.txt") or die "$!";
  4.  
  5. LOOP: while(<FILE>) {
  6.     if($counter = 0){
  7.         $counter++;
  8.         next LOOP;
  9.     }
  10.  
  11.     .... the rest of your code...
  12. }
  13.  
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
Sep 24 '08 #3
my bad,
just when I put the question on the forum, the answer rushed into my head:

Expand|Select|Wrap|Line Numbers
  1. open <FILE, 'text1'>;
  2. open <FILE2, 'text2'>;
  3.  
  4. $i = 0;
  5.  
  6. while (<FILE>) {
  7.    if ($i != 0) {
  8.        print FILE2 "$_\n";
  9.    }
  10.    $i++;
  11. }
  12.  
  13. close (FILE1);
  14. close(FILE2);

do u think this way is ok?

thanx so much for your help :D
Sep 24 '08 #4
numberwhun
Expert Mod 2GB
my bad,
just when I put the question on the forum, the answer rushed into my head:

Expand|Select|Wrap|Line Numbers
  1. open <FILE, 'text1'>;
  2. open <FILE2, 'text2'>;
  3.  
  4. $i = 0;
  5.  
  6. while (<FILE>) {
  7.    if ($i != 0) {
  8.        print FILE2 "$_\n";
  9.    }
  10.    $i++;
  11. }
  12.  
  13. close (FILE1);
  14. close(FILE2);
  15.  
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
Sep 24 '08 #5
a mistake, it should be
Expand|Select|Wrap|Line Numbers
  1. open(FILE2, '>text2')
so that we can write on file text2
Sep 24 '08 #6
KevinADC
Expert 2GB
This is much more efficient:


Expand|Select|Wrap|Line Numbers
  1. open(IN, "<file.txt") or die "$!";
  2. open (OUT, ">", 'outfile.txt') or die "$!";
  3. <IN>; #<-- discard first line
  4. print OUT <IN>;
  5. close(IN);
  6. close(OUT);
Sep 24 '08 #7
KevinADC
Expert 2GB
That's what I love about Perl..... TMTOWTDI !!!

Expand|Select|Wrap|Line Numbers
  1. my $counter = 0;
  2.  
  3. open(FILE, "<file.txt") or die "$!";
  4.  
  5. LOOP: while(<FILE>) {
  6.  
  7.  
  8.         $counter++;
  9.         next LOOP;
  10.     }
  11.  
  12.     .... the rest of your code...
  13. }
  14.  
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:

Expand|Select|Wrap|Line Numbers
  1.     if($counter = 0){
I think you can see why Jeff.
Sep 24 '08 #8
numberwhun
Expert Mod 2GB
This line is wrong:

Expand|Select|Wrap|Line Numbers
  1.     if($counter = 0){
I think you can see why Jeff.

He he, my quick, on the fly, untested code. Yes, it should be ==, sorry. :)
Sep 24 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

22 posts views Thread by Ling Lee | last post: by
19 posts views Thread by Alex Vinokur | last post: by
88 posts views Thread by Peter Olcott | last post: by
3 posts views Thread by Matthew Warren | last post: by
24 posts views Thread by Bill | last post: by
1 post views Thread by NeoGregorian | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.