CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Adding New Record

 



stanley
Novice

Jun 27, 2000, 7:10 AM

Post #1 of 4 (2990 views)
Adding New Record Can't Post

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

Post #2 of 4 (2990 views)
Re: Adding New Record [In reply to] Can't Post

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

Post #3 of 4 (2990 views)
Re: Adding New Record [In reply to] Can't Post

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


errr
Deleted

Jul 5, 2000, 7:50 PM

Post #4 of 4 (2990 views)
Re: Adding New Record [In reply to] Can't Post

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

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives