http://www.perlmonks.org?node_id=520168

trailmonk has asked for the wisdom of the Perl Monks concerning the following question:

We are hoping that an end user will be able to click on a URL link and for that link to point to the last record of our Perl database.

I'm not sure if this helps but, right now, our display.cgi code reads:

# Read in Data ############################### open (DATABASE, "$data_file"); @data=<DATABASE>; close (DATABASE); chomp @data;
We could reverse the way the data is displayed by adding:
@reverse = reverse @data; @data = @reverse;
but this is a quick fix and it is not what we truly desire. We desire that the end user has an option to click on a URL link so that only the last record is displayed. Do you have an perls of wisdom about this?

jdporter added code tags

Replies are listed 'Best First'.
Re: Display last record
by davidrw (Prior) on Dec 31, 2005 at 17:35 UTC
    There's no need for a temporary array when doing:
    @reverse = reverse @data; @data = @reverse;
    You can just do: @data = reverse @data and then print $data[0];. But to get the last item, you don't need to reverse the list -- just use the $#data or -1 index .. these are all the same:
    print $data[-1]; print $data[$#data]; print pop @data; # NOTE: THIS SHORTENS THE ARRAY print scalar splice(@data,-1); # NOTE: SAME AS pop # or: @data = reverse @data; # OF COURSE, @data IS NOW MODIFIED print $data[0]; print shift @data; # NOTE: THIS SHORTENS THE ARRAY print scalar splice(@data,0,1); # NOTE: SAME AS shift


    All of that is assuming you read your whole file into an array in memory, which is obviously costly for memory if the file is large ... A quick search revealed Read Last Line of A File Only
      Don't forget @data can be eliminated totally with (<DATABASE>)[-1]. It still loads the whole file into memory, though.
Re: Display last record
by ikegami (Patriarch) on Dec 31, 2005 at 21:20 UTC
    Have you looked at File::ReadBackwards? It reads a file line by line from the end of the file. It does so efficiently. It doesn't read the entire file, much less load it entirely into memory.

      Just to add to what ikegami said, File::ReadBackwards will not only read the file line by line, starting at the end of the file and working backwards, but it will read record by record (which usually is line by line, but doesn't have to be). This is a pretty flexible solution. In fact, unlike the plain old <> operator, File::ReadBackwards allows you to set the record separator based on either literal text, or a regular expression. Nice!


      Dave

Re: Display last record
by TedPride (Priest) on Dec 31, 2005 at 18:32 UTC
    Just move the file pointer to the end of the file minus whatever the maximum record size is, then read everything to the end of the file and extract the last record. This should be quite efficient even with huge files.

      Just move the file pointer to the end of the file minus whatever the maximum record size is, then read everything

      Without example code to back it up, this must look quite a bit obscure to a newbee.

      Being concerned of large files I would opt for the answer in perlfaq5 to perldoc -q "a line in a file" and use Tie::File.

      Cheers, Sören

Display last record
by trailmonk (Initiate) on Jan 05, 2006 at 05:05 UTC
    Thank you for your help. I think that I did not state my question accurately. What I *believe* will have to happen is that the end user will click on the "URL" and it will point to a subroutine. The subroutine will be an if->then->else, n+1 that searches line-by-line until it reaches the end of the file. At that point it will "print" or display the last record. I'm just guessing. You are awesome and I am grateful for your help.