in reply to Re: How do I copy out the last line written in a file?
in thread How do I copy out the last line written in a file?

When a file is read into an array the last element in the array array-1 would give you what you are looking for. Shashidhar Iddamsetty
  • Comment on Re: Answer: How do I copy out the last line written in a file?

Replies are listed 'Best First'.
Re^2: Answer: How do I copy out the last line written in a file?
by blazar (Canon) on May 03, 2007 at 10:18 UTC
    When a file is read into an array the last element in the array array-1 would give you what you are looking for.

    To put it more clearly, and in actual Perl rather than some pseudo-language, the first example above, i.e.

    @array = <FILE>; $last_line = $array[$#array];

    could be rewritten like

    @array = <FILE>; $last_line = $array[-1];

    or even without creating an intermediate @array:

    my $last_line = (<FILE>)[-1];

    Incidentally, I put a my in front of that as a reminder of the good rule of always staying strict.