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: Beginner:
Line number of EOF

 



xMattHawkx
stranger

Jun 17, 2001, 8:46 AM

Post #1 of 3 (901 views)
Line number of EOF Can't Post

Hi -

I was just wondering how I could determine the line number of the very last line in a file. Thanks!

xMattHawkx



randor
User

Jun 17, 2001, 2:05 PM

Post #2 of 3 (892 views)
Re: Line number of EOF [In reply to] Can't Post

xMattHawkx

try this:

open (FILE "file.txt");
@list = <FILE>;
close FILE;
$amount = grep(/\n/, @list); # this will work if you have one line per item needed.
$amount--; #this decriments the number by 1 to make it compatable with starting at 0
print qq~ the last line in the file is @list[$amount]\n~;

i havent tested this, but it should work.

also, if you are familiar with unshift, you could use that function to reverse the order, and then pull out the first, which actually is the last. but that is only if it fits your needs.




I Hope this helps...

perl programmers don't die.. they just start writing a new script.


PerlKid
stranger

Jun 17, 2001, 7:44 PM

Post #3 of 3 (887 views)
Re: Line number of EOF [In reply to] Can't Post

Loading the whole file into memory like that is not wise...especially since all you're doing is finding how many lines there are.


Code
open (DB, $file) or die $!; 
while (<DB>) { $eof++}
close (DB);

--Drew
http://www.camelsoup.com

Code
s;[\d\$&(\^)];;g+s;\.; ;g+s;(.)(..);$2$1;g+print,if$_='&61k4I.)l6il.edn7(K2e^ny$';


 
 


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

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