Forum Moderators: coopster & phranque

Message Too Old, No Replies

Puting data in the first line of a file

I have data being put in the bottom but I need it to go in the first line

         

FlyingWulf

6:07 am on May 28, 2005 (gmt 0)

10+ Year Member



Hi,

I have hava a small flat file database (an article script) and I want to have the entires in the db to be put in the first line instead of the bottom

currently this is the "code"

[code]
open (DATABASE,">>$data_server_path$articles_libris_database");
print DATABASE join "\¦", $itemid, $FORM{'name'}, $FORM{'author'}, $FORM{'content'}, $FORM{'image'}, $FORM{'contactemail'}, $FORM{'itemurl'}, $FORM{'group'}, $FORM{'date'}, $FORM{'empty_var_one'}, $FORM{'created_on'}, $FORM{'custom'}, $FORM{'empty_var_two'}, $FORM{'idle_2'}, $FORM{'image_caption'}, $FORM{'idle_4'},"\¦\n";
close(DATABASE);
[\code]

I have searched online and it seems the many pages and links I found none had the information I was looking for or the website was gone /dead

Any suggestions would be appreciated
Have a wonderful day

FlyingWulf
Learning as I stumble on my shoe strings

lexipixel

11:00 am on May 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You write to the end, but reverse them when you want to read the data...

#
open (SNEW, "$News_Article_DB");
seek (SNEW,0,0);
@lines = reverse <SNEW>; # reverse for 'LIFO'
close (SNEW);
#
foreach $line (@lines) {
#
# do stuff like;
# only print 'x' number of lines
# or lines with certain data in them...
#
}
#

If the data file grow very large and gets hit often, you might want to archive off chunks of it every so often.

...first one to say "What's SNEW?" gets thwapped in the head with a SS-SD 5 1/4" floppy....<grin>

bennymack

7:24 pm on May 28, 2005 (gmt 0)

10+ Year Member



I agree that you should write to the end of the file but then when reading in the file, you might find the "tac" command helpful. It is the same as "cat" but reverse. It's part of the GNU toolkit I beleive. Then you can open a filehandle to the tac process. Of course you won't be able to seek on the process handle but you hopefully won't need to with this method.