Solved

How to edit and delete records from flat file in perl

Posted on 2000-04-04
5
359 Views
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
Field1|!!|field2|!!|field3|!!|field4
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....
0
Comment
Question by:kifah
  • 2
  • 2
5 Comments
 
LVL 10

Expert Comment

by:makerp
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
0
 
LVL 84

Expert Comment

by:ozo
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"
or
tie %hash,$DBM,$file;
0
 

Author Comment

by:kifah
ID: 2690907
Hi,
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....
0
 
LVL 10

Accepted Solution

by:
makerp earned 100 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
$DBI_DRIVER = "ODBC";

# the dsn info
$DATA_SOURCE = "test_db";
$DATA_SOURCE_USERNAME = "";
$DATA_SOURCE_PASSWORD = "";

# global database handle for this script
my $dbh;

# connect to database, the first parameter will take the form of 'dbi:ODBC:test_db'
$dbh = DBI->connect('dbi:'.$DBI_DRIVER.':'.$DATA_SOURCE,$DATA_SOURCE_USERNAME,$DATA_SOURCE_PASSWORD)
      || 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'};
for($i=0;$i<$sth->{'NUM_OF_FIELDS'};$i++)
{
      #print("field : @{$fi[0]}[$i]\n");
       print("field : $fi->[$i]\n");
}
print("\n\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");
DBI::dump_results($sth);

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
$sth->finish();

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
$sth->finish();

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");
}
$sth->finish();

print("\n\tBye bye.... Shutting up shop !!!!");
# closes the database
$dbh->disconnect();

 

 
0
 

Author Comment

by:kifah
ID: 2696048
Thanks for that.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now