Home: Perl Programming Help: Advanced:
Adding New Record



stanley
Novice

Jun 27, 2000, 7:10 AM


Views: 5103
Adding New Record

Can anyone here please tell me how do I add new record between two existing records in flat file database?

Example :

Record Line 1
Record Line 2

I need to add new record in between line 1 and line 2 instead of under line 2, please help, thanks.


RayStreet
Deleted

Jun 27, 2000, 4:54 PM


Views: 5103
Re: Adding New Record

Open the existing file as input and a new file as output. Read thru the existing file writing to the output file as you're going. Write the new record into the output file where you want it. At the end close the files and then rename the new file to the same name as the old file so that it takes over as the existing file.


stanley
Novice

Jun 27, 2000, 7:25 PM


Views: 5103
Re: Adding New Record

Thank you, this really help me a lot...brilliant idea...thanks again


errr
Deleted

Jul 5, 2000, 7:50 PM


Views: 5103
Re: Adding New Record

Another case of RTFFAQ
How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file? http://www.perl.com/pub/doc/manual/html/pod/perlfaq5.html#How_do_I_change_one_line_in_a_fi

A filesystem has no concept of a "line".
To insert a line, you either need to rewrite the whole thing, or use something like DB_File and tie the file to an array.
Also consider DBD::CSV if you really want a flat file database.. You get some basic functionality.

DB_File docs at http://search.cpan.org/doc/PMQS/DB_File-1.73/DB_File.pm or of course perldoc DB_File from the command line. Its standard distro.

DBD::CSV docs at http://search.cpan.org/doc/JWIED/DBD-CSV-0.1022/lib/DBD/CSV.pm You would need to install DBI and DBD but then you get a pretty cool text database