• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 382
  • Last Modified:

How to edit and delete records from flat file in perl

I have a flat file which I store some data in it and each record in a line like this
note: |!!| is the field separator.

This file contain about 4000 records and can be more (4000 line because each line is a record)

so what I want to do is:
1- to edit one of the records (lets say record 3476)
2- to delete one of the records (lets say record 3000).

so what is the perfect way to do so.......
do I have to rebuild the file which contain 4000+ record, and it could be more.
or there is a better way to do so....
Please give code for both ways.....

Wait.... I will run the cgi on a NT web-server....
  • 2
  • 2
1 Solution
the beter way is to use a database and DBI to talk to it ..

its easy. 4000 + records > use access. you dont even need access on the server

write a script to read through your existing file pumping your existing data into the db and then use the db from then on
perldoc -q "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"
tie %hash,$DBM,$file;
kifahAuthor Commented:
I could not understand both of you.

Mr. makerp
What database you are talking about, and what is the DBI... can you please give more details....

Mr. ozo
where I can find this document.. can you give me a link to this page....

please I prefer a code....
all i mean is create a database. such as access (your on NT). design it based on your flat file and them write a script to take the data from the flat file and pump it into the db, then use the db from then on and scrap your file.

databases make life so mush easier. you will have less code to maintain as the sql can do alot of the work for you. also it will be a mush more robust solution and make enhacments to your app alot easier.

dbi is a perl module that provides a frame work for database access. dbd modules implement the methods for dbi but for a specific db. therefore you install dbi and then decide which database you want to use then get the appropriate dbd driver for it.

if you going to be on NT then i suugest activeperl and the go to the /packages/zips/ dir of there web site and get dbi and install it. then get the DBD-ODBC module. this will be fine for any odbc complient data store . i.e access/sql server/oracle etc.

below is a comprehensive example of dbi. it uses an access data base made up of a single table called table1 with two fileds name and addess. set an odbc link up in contol panel called test_db and point it to the db you have created. put some junk data in the db fileds.

# make use of the dbi module. for activeperl go to www.activestate.com/packages/zips and
# download the DBI package first and then download what ever DBD drivers you what to use
# this script makes use of the odbc driver module
use DBI;

# the dbd driver we actually want to use

# the dsn info
$DATA_SOURCE = "test_db";

# global database handle for this script
my $dbh;

# connect to database, the first parameter will take the form of 'dbi:ODBC:test_db'
      || die("Connection error : ".$DBI::errstr);

# set the LongReadLen really high incase we have meno fields
$dbh->{LongReadLen} = 30000;

# prepare a stmt, this returns a statment handle
$sth = $dbh->prepare("SELECT * FROM table1") || die("Stmt error : ".$dbh->errstr);

# use some of the stmt handle vars to determine the fields that
# will be returned. this is usfull if we dont know in advance
# the number/name of fields that will be returned i.e 'select *'
print("Expected fields to be returned from first query.......\n");
$fi = $sth->{'NAME'};
      #print("field : @{$fi[0]}[$i]\n");
       print("field : $fi->[$i]\n");
# execute our stmt
$sth->execute() || die("Stmt error : ".$dbh->errstr);

# if we are debugging then we may wish to dump our result in one hit
# the dump_results function uses default values for row delimiter's etc
# these can be overridden
# dump_results(st_handle,maxlen,row_delimiter,field_delimiter,fileh_for_dump)
print("Dump of the results returned by stmt, for debugging purposes\n");

print("\nNormal results retrival using a loop to go through each row returned\n");
# execute our stmt
$sth->execute() || die("Stmt error : ".$dbh->errstr);

# fetch and print the rows, returns undef on failure, hash reference on success
while($dat = $sth->fetchrow_hashref())
      # get the name field from our hashref
      print("Name : ".$dat->{"name"}."\t Address : ".$dat->{"address"}."\n");

# finish the statement handle, if we dont do this the disconnect function will spew some errors

print("\nDoing some inserts into the database\n");
# set auto commit to false so we have to explicetly call commit on the database handle
$dbh->{"AutoCommit"} = 0;
# we can prepare a stmt and execute it several times with different values in the place holders like this
$sth = $dbh->prepare("INSERT INTO table1 (name,address) VALUES (?,?)") || die("Stmt error : ".$dbh->errstr);
# execute it but this time pass the values we want to replace the place holders
$sth->execute("bart","saturn") || die("Stmt error : ".$dbh->errstr);
# commit it
$dbh->commit() || die("Stmt error : ".$dbh->errstr);
# turn auto commit back on
$dbh->{"AutoCommit"} = 1;
# finish with the stmt handle

print("\nDoing an update on all records (changing address to mars)\n");
# we can use the do function to execute a stmt and get the number of rows it affected
$rows = $dbh->do("UPDATE table1 SET address = 'mars'") || die("Stmt error : ".$dbh->errstr);
print("That affected ".$rows." rows\n");

# now print the updated table out
print("\nUpdated table contents are : \n");
$sth = $dbh->prepare("SELECT * FROM table1") || die("Stmt error : ".$dbh->errstr);
$sth->execute() || die("Stmt error : ".$dbh->errstr);

while($dat = $sth->fetchrow_hashref())
      # get the name field from our hashref
      print("Name : ".$dat->{"name"}."\t Address : ".$dat->{"address"}."\n");

print("\n\tBye bye.... Shutting up shop !!!!");
# closes the database


kifahAuthor Commented:
Thanks for that.

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now