More Efficient Way To Get Last Line From A TXT File 
Author Message
 More Efficient Way To Get Last Line From A TXT File

Hello All,

I'm a new user of Perl and would like to get some help/advice on how to improve
my Perl program efficiently.

I have a project which requires me to read the LAST LINE of my data file every 5
minutes.

My data file structure looks like this:-

"(PDH-CSV 4.0) (Malay Peninsula Standard
Time)(-480)","\\BDC302CTX14\Processor(_Total)\% Processor Time"
"10/25/2002 15:08:13.358","99.997787000987586"
"10/25/2002 15:13:13.358","45.777343733333332"
"10/25/2002 15:18:13.358","41.129557299999995"
"10/25/2002 15:23:13.358","39.420572933333332"
"10/25/2002 15:28:13.358","41.313802066666675"
"10/25/2002 15:33:13.358","47.742838533333334"
"10/25/2002 15:38:13.358","28.370442733333334"
"10/25/2002 15:43:13.358","34.31054686666667"

My Perl source:-

   if ( not open( CSVFILE, $cCSVFile ) )
   {
      print qq(ERROR! ERROR!\n);
      print qq(Cannot Open PERFMON CSV File: $cCSVFile For Reading: $!\n\n);

      exit( 1 );
   }

   #----------------------------------------------------#
   # Read ALL PERFMON CSV Data And Store Into Array     #
   # And Get The Last Line Of Data                      #
   # NOTE: BE Careful Here!! Storing ALL Data From File #
   #       Into Array! Will EAT Up RAM!!                #
   #----------------------------------------------------#



Is there anyway i can improve the last three lines of the above code and not
using ARRAY to store all the contents of the file?

Thanks In Advance.

Johnny Lim
Malaysia



Sun, 17 Apr 2005 01:54:01 GMT  
 More Efficient Way To Get Last Line From A TXT File
Johnny Lim announced:

Quote:
> Hello All,

> I'm a new user of Perl and would like to get some help/advice on how to improve
> my Perl program efficiently.

> I have a project which requires me to read the LAST LINE of my data file every 5
> minutes.

> My data file structure looks like this:-

> "(PDH-CSV 4.0) (Malay Peninsula Standard
> Time)(-480)","\\BDC302CTX14\Processor(_Total)\% Processor Time"
> "10/25/2002 15:08:13.358","99.997787000987586"
> "10/25/2002 15:13:13.358","45.777343733333332"
> "10/25/2002 15:18:13.358","41.129557299999995"
> "10/25/2002 15:23:13.358","39.420572933333332"
> "10/25/2002 15:28:13.358","41.313802066666675"
> "10/25/2002 15:33:13.358","47.742838533333334"
> "10/25/2002 15:38:13.358","28.370442733333334"
> "10/25/2002 15:43:13.358","34.31054686666667"

> My Perl source:-

>    if ( not open( CSVFILE, $cCSVFile ) )
>    {
>       print qq(ERROR! ERROR!\n);
>       print qq(Cannot Open PERFMON CSV File: $cCSVFile For Reading: $!\n\n);

>       exit( 1 );
>    }

>    #----------------------------------------------------#
>    # Read ALL PERFMON CSV Data And Store Into Array     #
>    # And Get The Last Line Of Data                      #
>    # NOTE: BE Careful Here!! Storing ALL Data From File #
>    #       Into Array! Will EAT Up RAM!!                #
>    #----------------------------------------------------#



> Is there anyway i can improve the last three lines of the above code and not
> using ARRAY to store all the contents of the file?

> Thanks In Advance.

> Johnny Lim
> Malaysia

use File::ReadBackwards;

HTH,
Dominik



Sun, 17 Apr 2005 02:50:13 GMT  
 More Efficient Way To Get Last Line From A TXT File
On Wed, 30 Oct 2002 02:50:13 +0100, Dominik Seelow

< SNIP >

Quote:

>use File::ReadBackwards;

>HTH,
>Dominik

Dominik,

Thanks. Found earlier in Google and downloaded from CPAN. :)



Sun, 17 Apr 2005 06:32:45 GMT  
 More Efficient Way To Get Last Line From A TXT File


Quote:




> Is there anyway i can improve the last three lines of the above code and
not
> using ARRAY to store all the contents of the file?

When you evaluate a filehandle in scalar context, you will get the next line
of the file, or undef if there are no more lines.  For example:

my $next_line_in_file = <CSVFILE>;

This is the most common way of using file handles, as "slurping" does not
scale very well.  Look at the "I/O Operators" section in "perldoc perlop".
Something that would work for you is:

my $last_line; # this will hold the last line successfully read
while(<CSVFILE>) { # cycle through whole file
   $last_line = $_; # the data returned by <CSVFILE> is stored in $_

Quote:
}

print "> Last line: $last_line\n";

--Ben Kennedy



Sun, 17 Apr 2005 18:47:37 GMT  
 More Efficient Way To Get Last Line From A TXT File
Good day Ben,



<SNIP>

Quote:
>This is the most common way of using file handles, as "slurping" does not
>scale very well.  Look at the "I/O Operators" section in "perldoc perlop".
>Something that would work for you is:

>my $last_line; # this will hold the last line successfully read
>while(<CSVFILE>) { # cycle through whole file
>   $last_line = $_; # the data returned by <CSVFILE> is stored in $_
>}
>print "> Last line: $last_line\n";

>--Ben Kennedy

your above example do have a valid usage to get the last line of a file. But if
the file is large, then it's going to take some time to reach to the bottom of
the file.

What i have used is File::ReadBackwards which is available at CPAN. It uses seek
instead of reading line by line.

Anyway, thanks for info.

Johnny Lim



Mon, 18 Apr 2005 04:06:30 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. how to delete last line in .txt.file

2. most efficient way to find the LAST line number in a file

3. Even without binmode, txt file gets uploaded as BINARY instead of ASCII

4. txt files getting denied.

5. Specifying *.txt as the input file on the command line

6. reading a txt file and inserting text lines in a HTML

7. overwriting one line in txt file

8. help newbie : how to count line into a txt file

9. Getting info of file last update

10. better way of getting the last modified file?

11. WinNT: getting File Last Modified data from outside current directory

12. Renaming files *.txt to 1234.txt

 

 
Powered by phpBB® Forum Software