Re: Reversing the order of a flat file in Perl

by "Srinivasan Ramakrishnan" <srinivar(at)md3.vsnl.net.in>

 Date:  Mon, 3 Jul 2000 17:04:31 +0530
 To:  <Nick(at)nlewis.freeserve.co.uk>,
"Jan Morell" <janman(at)n-jcenter.com>,
<hwg-languages(at)hwg.org>
 References:  co
  todo: View Thread, Original
Hi,

I know you are doing this in Perl, but if you are interested I have atleast
3 programs which I have written in C to reverse a file.

If you want I can give you the code or the exe. Either using the code you
can write a file reversal in Perl, or simply use my exe which accepts
command line params, and run it as a system process everytime your file
needs reversal.

The code is strict ANSi, so it should run on any platform if compiled with
an ANSI compliant compiler.

-Srini


----- Original Message -----
From: Nick <Nick(at)nlewis.freeserve.co.uk>
To: Jan Morell <janman(at)n-jcenter.com>; <hwg-languages(at)hwg.org>
Sent: Monday, July 03, 2000 11:57 AM
Subject: Re: Reversing the order of a flat file in Perl


| Hi all
|
| If you genuinely need to append to the top of the file you will have to
copy it all in the
| process. ie open a new file, print the new data, copy the old file to the
new file, close
| both files, delete the old file and finally rename the new file to the old
file name. Clumsy!!
|
| An alternative if the file is not too large is to read the file by
slurping it all into an array with
|
| open(MYFILE,myfile);
| @array=<MYFILE>;
| close(MYFILE);
|
| and read the elements like this:
|
| $array[scalar(@array)-1] is the last line appended to the file
| $array[scalar(@array)-2] is the last line but one
| ..............
|
| If you prefer once the data is in the array you can simply reverse the
order
| with:
|
| @array=reverse @array;
|
| before reading it!
| If you are going to read more than a few elements this is probably
| quicker anyway
|
| Hope this is relevant and some help.
|
| Nick Lewis
| World Class Web Sites
| for any business
| http://www.nlnet.co.uk
|
|
|
| At 02/07/00 09:31:00, you wrote:
| >Hi Listmembers,
| >
| >Does anyone know of a way to "append" to the head of a flat file instead
of
| >end?
| >
| >I am creating a listing in which the output of a flat database file is
read
| >into an HTML page using SSI.  The file is created by a form and each
entry
| >is now appended to the end of the database.  I would like the new entry
to
| >be written to the head of the file instead.  "Reverse Cronology" so to
| >speak.
| >
| >I am on a UNIX server, running Apache and Perl 5.004.
| >
| >As an alternative I was thinking of trying to write a Perl routine to
| >reverse the order of the file and call that in the SSI instead of just
| >importing the file, but I would rather get the import file in the order
that
| >I want first so that I don't have to call another script to get the same
| >results.
| >
| >Example:
| >
| >Now the database looks like this:
| >
| >URL 1<br>Description1<br>
| >URL 2<br>Description2<br>
| >URL 3<br>Description3<br>
| >
| >I want it to look like this:
| >
| >URL 3<br>Description3<br>
| >URL 2<br>Description2<br>
| >URL 1<br>Description1<br>
| >
| >Thanks in advance for any help.
| >
| >Jan
| >
| >
| >
| >
| >
| >
|
|

HWG: hwg-languages mailing list archives, maintained by Webmasters @ IWA