Gossamer Forum
Home : General : Perl Programming :

database question

Quote Reply
database question
Usually, when you add a new line to the database, it adds at the end (last line).
Is there a way to add a line to the top (first line)?

Thank you.


Pasha

------------------
webmaster@find.virtualave.net
http://find.virtualave.net
Quote Reply
Re: database question In reply to
There's a way to do it. If you're talking about DBMan, you would have to make changes in the add_record subroutine.

It seems like a lot of work, though. Can I ask why you want to change it? You can set up the script to display records in any order you want.

But, since you want it, I think this will work. It's completely untested though, so be sure to try it out on a database file you can afford to lose.

Instead of

Code:
if ($status eq "ok") {
open (DB, ">>$db_file_name") or &cgierr("error in add_record.
unable to open database: $db_file_name.\nReason: $!");
if ($db_use_flock) {
flock(DB, 2) or &cgierr("unable to get exclusive lock on $db_file_name.\nReason: $!");
}
print DB &join_encode(%in);
close DB; # automatically removes file lock

use

Code:
if ($status eq "ok") {
open (DB, "<$db_file_name") or &cgierr("error in add_records.
unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>; # Slurp the database into @lines..
close DB;
$output = &join_encode(%in);
foreach $line (@lines) {
$output .= $line;
}
open (DB, ">$db_file_name") or &cgierr("error in add_records.
unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) {
flock(DB, 2) or &cgierr("unable to get exclusive lock on $db_file_name.\nReason: $!");
}
print DB $output;
close DB;


------------------
JPD







[This message has been edited by JPDeni (edited May 04, 1999).]
Quote Reply
Re: database question In reply to
Yes?
Code:
open (DB, "<$db_file_name");
@lines = <DB>;
close DB;

$output = &join_encode(%in);

open (DB, ">$db_file_name");
print DB $output;
print DB @lines;
close DB;
Quote Reply
Re: database question In reply to
I guess that will work. I've had problems in the past trying to print out an array by just using

print @array;

but I just tried it and it worked fine. I'll have to go back and see if I can figure out what the problem was before.



------------------
JPD





Quote Reply
Re: database question In reply to
JPDeni, it's not for DBMan. It's just simple:

Code:

open (DATABASE_IN,">>database.txt");
print DATABASE_IN "$field0|$field1|$field2|$field3|$field4|$field5\n";
close(DATABASE_IN);


boyss, in your case the script will take all the data from the file in the memory, then it will first print the new data, and then old data (from the memory). I think it will take less time for this operation if the script would just add a new data at the top of the file. Smile


Regards,

Pasha

------------------
webmaster@find.virtualave.net
http://find.virtualave.net
Quote Reply
Re: database question In reply to
Sorry. I misunderstood.

I don't think there is a command that will add data to the beginning of a file. The ">>" means to "append" -- always at the end of the file. As far as I know, the only way to add data to the beginning of the file is to read in the old data, write the new data and then write the old data.

I've looked through my books on Perl and I couldn't find anything that indicated a way to write new data to the beginning of a file.


------------------
JPD





Quote Reply
Re: database question In reply to
What you would have to do is:

-Read the contents of the file which you want to add to
-Print the new line to a new file
-print the contents of the file which you want to add to
-close both files
-delete the first file
-rename the new file to the name of the previous file.

Its kinda messy, but it would work.

------------------
Quote Reply
Re: database question In reply to
Hi Pasha,

As JPD says, afaik there's no way to print to the start of a file (which means you wouldn't be able to print your line break there either). You have to do pretty much what the others have been telling you. Put your new record into a variable, then read the data from the file, append it to that data and then write it back to the file.

This is how I would do it:

Code:
$newdata = "field0|field1|field2|field3|field4|field5\n";
open (DB, "$dbfile") or &your_error_routine;
@olddata = <DB>;
close(DB);
foreach $record (@olddata) { $newdata .= $record; }
open (DB, ">$dbfile") or &your_error_routine;
print DB $newdata;
close(DB);

Sorry about the closeknit coding, that's how I like to do it. You might possibly be able to read and write to the database without having to open it twice, but I don't think so. Don't forget to change $dbfile and &your_error_routine to whatever is appropriate.

Cheers,
adam
Quote Reply
Re: database question In reply to
Code:
open (file, "$file_name") or &error("Unable to open the file.");
@file=<file>;
close (file);

$line=0;
foreach $file (@file) {$line++;

if ($file=~ /<!--marker-->/i) {$line--;
@add="<!--marker-->\n string to be added\n";

splice (@file, $line, 1, @add);

open (sfile, ">$file_name") or &error("Unable to write to the file.");
print sfile @file;
close (sfile);
}
}
Think I got it right. If your unique marker was at the top of your file
any additions to the file would be added to the top of the file, well not
quite the top as the marker is the top line. <!--marker--> is transparent
in html, not sure how you could add to the tippy top of a text file. The
marker could be anyplace and it could be at the beginning or end of @add,
putting the string below or above the marker.
...Marker could also be any uniqe string. If the top line is unique and you
only want to add to the top once you could use it as the marker (match string)
and put it at the end of @add.

[This message has been edited by Dave (edited May 06, 1999).]
Quote Reply
Re: database question In reply to
Jimz, yes it is messy Smile

As I understand \n will add a blank line at the end of the file. If there would be a way to add a blank line at the beginning of the file, then (may be) it will print new data to this blank line.

Guys, let me know if this would work.


Regards,

Pasha

------------------
webmaster@find.virtualave.net
http://find.virtualave.net