Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


How to edit and delete records from flat file in perl

Posted on 2000-04-04
Medium Priority
Last Modified: 2011-09-20
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....
Question by:kifah
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 10

Expert Comment

ID: 2683710
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
LVL 84

Expert Comment

ID: 2684870
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;

Author Comment

ID: 2690907
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....
LVL 10

Accepted Solution

makerp earned 400 total points
ID: 2692565
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



Author Comment

ID: 2696048
Thanks for that.

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question