CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Remove First line from File

 



andy7t
User

Jan 30, 2004, 9:29 AM

Post #1 of 5 (956 views)
Remove First line from File Can't Post

Hello,

I have a large file, with many lines in (about 50,000).

I need to remove the first line from that file each time it is run.

Normally i would consider:

$num=0;
foreach $line (@line)
{
next if $num eq "0";

print LIST "$line\n";
$num++;
}



However i think with 50,000 lines this is not a good idea.

Is it not possible just to remove the first line from a file simply?


davorg
Thaumaturge / Moderator

Jan 30, 2004, 10:46 AM

Post #2 of 5 (954 views)
Re: [andy7t] Remove First line from File [In reply to] Can't Post

The easiest way is probably to look at Tie::File which has been included with Perl since version 5.8.0.

But your code can be made a bit simpler


Code
print LIST map { "$_\n" } @line[1 .. $#line];

Or perhaps

Code
shift @line; 
print LIST map { "$_\n" } @line;


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Recall
Novice

Jan 30, 2004, 5:56 PM

Post #3 of 5 (952 views)
Re: [davorg] Remove First line from File [In reply to] Can't Post

...which is a memory hog for 50,000 lines.


davorg
Thaumaturge / Moderator

Jan 31, 2004, 1:07 AM

Post #4 of 5 (949 views)
Re: [Recall] Remove First line from File [In reply to] Can't Post

Of yes. Of course it is. But he'd already read the whole file into an array so the damage was done :)

But my first suggestion was Tie::File, which _wouldn't_ read the whole file into memory.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Recall
Novice

Jan 31, 2004, 4:07 AM

Post #5 of 5 (947 views)
Re: [davorg] Remove First line from File [In reply to] Can't Post


Quote
But he'd already read the whole file into an array so the damage was done :)



Then you should point out to him not to do that rather than allowing him to continue with it =)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives