Reading the last line of a file 
Author Message
 Reading the last line of a file

I am trying to get my program to read the last line of a file..
I have tried a loop but had no luck..does anyone have any suggestions?
here is a chunk of what I have so far which displays all the data in the
file..


close(FILE);


{

print "$ok[0]  $ok[1]  $ok[2]\n";

Quote:
}

exit (0);


Sun, 07 Nov 1999 03:00:00 GMT  
 Reading the last line of a file

: I am trying to get my program to read the last line of a file..
: I have tried a loop but had no luck..does anyone have any suggestions?
: here is a chunk of what I have so far which displays all the data in the
: file..

: open (FILE, $database);

: close(FILE);

Well, since you now have the whole file slurped into an array, all you
need to know is the index of the last element in the array.

Perl is ready to give this to you:

print $file[$#file];  #   $#file is the subscript of the last element

--
    Tad McClellan                          SGML Consulting
    Tag And Document Consulting            Perl programming



Sun, 07 Nov 1999 03:00:00 GMT  
 Reading the last line of a file

: I am trying to get my program to read the last line of a file..
: I have tried a loop but had no luck..does anyone have any suggestions?
: here is a chunk of what I have so far which displays all the data in the
: file..
:
: open (FILE, $database);

: close(FILE);

$lastline = $file[-1];

Hope this helps!

---------------------------------------------------------------------

 --*--    Home Page: http://www.cinenet.net/users/cberry/home.html
   |      Member of The HTML Writers Guild: http://www.hwg.org/  
       "Every man and every woman is a star."



Sun, 07 Nov 1999 03:00:00 GMT  
 Reading the last line of a file

+ I am trying to get my program to read the last line of a file..

One could use seek(), but that looks to be somewhat involved. Not horribly
so if you have a fixed record length.

+ open (FILE, $database);

You really should make your open()'s look like this:
   open( BLAH, $blah) or die $!

in case something bad happens.

+ close(FILE);

Ok, you slurp up the entire file. This is easy. The last line is:

$last_line=$file[$#file];


+ {

Dern C programmers... :)

+   $_ = $line;

Unnecessary. This form of ForEach uses $_ as the index:



Quote:
}

James

--
Consulting Minister for Consultants, DNRC
Support the anti-Spam amendment <url:http://www.cauce.org/>
To cure your perl CGI problems, please look at:
<url:http://www.perl.com/perl/faq/idiots-guide.html>



Sun, 07 Nov 1999 03:00:00 GMT  
 Reading the last line of a file



Quote:

> : I am trying to get my program to read the last line of a file..
> : I have tried a loop but had no luck..does anyone have any
suggestions?
> : here is a chunk of what I have so far which displays all the data
in the
> : file..
> :
> : open (FILE, $database);

> : close(FILE);

> $lastline = $file[-1];

or even

Quote:

> Hope this helps!

> ---------------------------------------------------------------------

>  --*--    Home Page: http://www.cinenet.net/users/cberry/home.html
>    |      Member of The HTML Writers Guild: http://www.hwg.org/  
>        "Every man and every woman is a star."



Mon, 08 Nov 1999 03:00:00 GMT  
 Reading the last line of a file

 I also must add there are always 30 lines in the file that is being
read.



Mon, 08 Nov 1999 03:00:00 GMT  
 Reading the last line of a file

|> I am trying to get my program to read the last line of a file..
|> I have tried a loop but had no luck..does anyone have any suggestions?
|> here is a chunk of what I have so far which displays all the data in the
|> file..

   Do you want all of the data, or just the last line?

|> open (FILE, $database);

|> close(FILE);

   This means that you allocate memory for all of the file.  If it is
big, you will need the memory.  Somewhat wastefull of resources.

 chomp($last_line = $_) while(<FILE>);

is all you need if you really do just want the final line.  (The chomp
is to remove the trailing newlines - if you want them then remove the
chomp())

--

The contents of this message *may* reflect my personal opinion.  They are
*not* intended to reflect those of my employer, or anyone else.



Mon, 08 Nov 1999 03:00:00 GMT  
 Reading the last line of a file


Quote:
(Craig Berry) writes:
> As near as I can tell from reading the 'perldata' doc, $list[$#list] and
> $list[-1] are entirely equivalent expressions.  

I didn't see that in  man perldata, and it isn't (entirely) true.

 h , d : a b c d e f g h


Mon, 08 Nov 1999 03:00:00 GMT  
 Reading the last line of a file

Everything is too complicate this is more easy for me.

open (LAST , "tail -1 $file |");
while (<LAST>)
{print "$_\n"}

The result will be the last record.

RC



Mon, 08 Nov 1999 03:00:00 GMT  
 Reading the last line of a file

Quote:

> I am trying to get my program to read the last line of a file..
> I have tried a loop but had no luck..does anyone have any suggestions?
> here is a chunk of what I have so far which displays all the data in the
> file..

> open (FILE, $database);

> close(FILE);


> {
>   $_ = $line;

> print "$ok[0]  $ok[1]  $ok[2]\n";
> }
> exit (0);


last element of the array. Then continue your process.


  print "$ok[0]  $ok[1]  $ok[2]\n";
  exit (0);

Dan Boorstein



Mon, 08 Nov 1999 03:00:00 GMT  
 Reading the last line of a file




: > : I am trying to get my program to read the last line of a file..
: > : I have tried a loop but had no luck..does anyone have any suggestions?
: > : here is a chunk of what I have so far which displays all the data in the
: > : file..
: > :
: > : open (FILE, $database);

: > : close(FILE);
: >
: > $lastline = $file[-1];
: or even

                    ^ Yes, if you change this to $

As near as I can tell from reading the 'perldata' doc, $list[$#list] and
$list[-1] are entirely equivalent expressions.  

---------------------------------------------------------------------

 --*--    Home Page: http://www.cinenet.net/users/cberry/home.html
   |      Member of The HTML Writers Guild: http://www.hwg.org/  
       "Every man and every woman is a star."



Mon, 08 Nov 1999 03:00:00 GMT  
 Reading the last line of a file


Quote:

>|> I am trying to get my program to read the last line of a file..
>|> I have tried a loop but had no luck..does anyone have any suggestions?
...
>|> open (FILE, $database);

>|> close(FILE);

This is fine for a tiny file, such as the 30-liner he's actually faced with.

Quote:
>   This means that you allocate memory for all of the file.  If it is
>big, you will need the memory.  Somewhat wastefull of resources.

> chomp($last_line = $_) while(<FILE>);

This is fine for a medium-sized file.

If you have a large file (say multiple GB), you'd better do something like:
#
open (FILE, $database) || die "cannot open:$!\n";
$filesize = -s FILE;
$start = $filesize - 512;      #or some number you're SURE will have >1 line
seek (FILE,$start,0) || die "cannot seek:$!\n";
chop($last_line = $_) while(<FILE>);
--
Bob Shair                          Open Systems Consultant

Champaign, IL 61821                217/356-2684
< Not employed by or representing the University of Illinois >



Tue, 09 Nov 1999 03:00:00 GMT  
 Reading the last line of a file

: Everything is too complicate this is more easy for me.
  ^^^^^^^^^^    ^^^^^^^^^^^^^

Including yours  ;-)

: open (LAST , "tail -1 $file |");
: while (<LAST>)
: {print "$_\n"}

print `tail -1 $file`;

: The result will be the last record.

--
    Tad McClellan                          SGML Consulting
    Tag And Document Consulting            Perl programming



Tue, 09 Nov 1999 03:00:00 GMT  
 Reading the last line of a file

:

Quote:
: (Craig Berry) writes:

:
: > As near as I can tell from reading the 'perldata' doc, $list[$#list] and
: > $list[-1] are entirely equivalent expressions.  
:
: I didn't see that in  man perldata, and it isn't (entirely) true.

:  h , d : a b c d e f g h

Yeah, I thought of this too, and almost posted a followup to point it out
-- but after all, $[ is deprecated, so I don't feel *too* bad about
ignoring it.  And the statement itself isn't in perldata, but you can
infer it from how $#list and $list[-1] are defined.

---------------------------------------------------------------------

 --*--    Home Page: http://www.cinenet.net/users/cberry/home.html
   |      Member of The HTML Writers Guild: http://www.hwg.org/  
       "Every man and every woman is a star."



Tue, 09 Nov 1999 03:00:00 GMT  
 Reading the last line of a file

Quote:

> I am trying to get my program to read the last line of a file..
> I have tried a loop but had no luck..does anyone have any suggestions?
> here is a chunk of what I have so far which displays all the data in the
> file..

> open (FILE, $database);

> close(FILE);


> {
>   $_ = $line;

> print "$ok[0]  $ok[1]  $ok[2]\n";
> }
> exit (0);

Assuming that this is on a Unix system, then I'd just do:

$line = `tail -1 $database` or die "Oops, tail dont work ($!)";

There is no dishonor in using a tailor-made unix command to do a job.
In perl, there must be a dozen different ways of getting the last
line.  But if it is a huge file and you really want just the last
line, just use the stinking unix command and save yourself some time
and headaches.

--
Clark Dorman                            "Evolution is cleverer than you are."
http://cns-web.bu.edu/pub/dorman/D.html                -Francis Crick



Tue, 09 Nov 1999 03:00:00 GMT  
 
 [ 19 post ]  Go to page: [1] [2]

 Relevant Pages 

1. read last line without reading previous lines, how?

2. Reading Last X Lines of File?

3. Reading last 3 lines from the file

4. Quickly read the last 10 lines of a very large logfile

5. read last line only...

6. best way to read last line?

7. [question: cannot delete the last line of a file]

8. How to get last line in ASCII file?

9. trim the last blank-line and compare files

10. killingthe last line of the file

11. More Efficient Way To Get Last Line From A TXT File

12. [Fwd: How can I get the last element on each line in a text file]

 

 
Powered by phpBB® Forum Software