EDITING DATAFILES - What is best of 3 ways? 
Author Message
 EDITING DATAFILES - What is best of 3 ways?

I have www site that permits users to enter data into a searchable datafile. I want people to be
able to change their own data themselves.  Each person therefore gets a "correction code" when
they fill out a listing. The combination of the email and correction code constitute a password to
permit editing a file.  This permits me to establish $oldcontent and $newcontent for each field in
a record.  

I have studied the options for EDITING datafiles and find that there are three basic ways to do
this.

1) Process the entire file by rewriting it with the changes made only to the authorized specific
records.  In other words, open file and rewrite each record as is, except the record that matches
the email address and the correctioncode fields. Modify those specific records and include them
in the rewrite.  Because the file is large, this seems to me to be a lot of unnecessary work for the
computer.

2) Rewrite the entire file using the -p and -i operator.  Esssentially this is the same thing as the
above...but may be more efficiently done this way because I do not think each record is actually
being re-processed.  I have experimented with this but cannot yet get it to edit the data.

3) Use of DBM and Associative Arrays.

What I would really like to do is to delete the specific record altogether and append the updated
record to the end of the file.

I would appreciate anyone's view of the best, most efficient way to do this.

Essentially, the database has variable fields and is in this form:

===========
email:address
field1:contents
field2:contents
field3:contents
field4:contents
correctioncode:code
===============

Thanks for your help.

Arthur Rosenfield



Sun, 14 Jun 1998 03:00:00 GMT  
 EDITING DATAFILES - What is best of 3 ways?

Quote:

>  I have www site that permits users to enter data into a searchable datafile. I want people to be
>  able to change their own data themselves.  Each person therefore gets a "correction code"
when
>  they fill out a listing. The combination of the email and correction code constitute a password
to
>  permit editing a file.  This permits me to establish $oldcontent and $newcontent for each field
in
>  a record.  

>  I have studied the options for EDITING datafiles and find that there are three basic ways to do
>  this.

>  1) Process the entire file by rewriting it with the changes made only to the authorized specific
>  records.  In other words, open file and rewrite each record as is, except the record that
matches
>  the email address and the correctioncode fields. Modify those specific records and include
them
>  in the rewrite.  Because the file is large, this seems to me to be a lot of unnecessary work for
the
>  computer.

>  2) Rewrite the entire file using the -p and -i operator.  Esssentially this is the same thing as the
>  above...but may be more efficiently done this way because I do not think each record is
actually
>  being re-processed.  I have experimented with this but cannot yet get it to edit the data.

>  3) Use of DBM and Associative Arrays.

>  What I would really like to do is to delete the specific record altogether and append the
updated
>  record to the end of the file.

>  I would appreciate anyone's view of the best, most efficient way to do this.

>  Essentially, the database has variable fields and is in this form:

>  ===========
>  email:address
>  field1:contents
>  field2:contents
>  field3:contents
>  field4:contents
>  correctioncode:code
>  ===============

>  Thanks for your help.

>  Arthur Rosenfield

Please advise me.

So far, all of the variations I try do not have the proper effect of editing a file.

I am trying to edit a datafile on the disk using a FORM to enter the change and then a PERL
script to locate a matched patter, and then edit the datafile on the hard drive.  It is a standard
procedure that I have studied in four books using the -p and -i options with the PERL script.  I
have tried several variation of the program, all to no effect.  I know the script is okay because I
am not getting any error messages. One thing might be file permissions.  Perhaps I am
supposed to be designating the file with a +> when I open it to read and write to it. However,  I
think this would wipe it out altogether.

The results are as follows.

If I use the -i option alone, the program writes data to the screen with the appropriate changes,
but does not edit the file in any way.

If I use a -pi together as a cluster, I get exactly the same result as above.

If I use a -pi.bak no change is made in the data, it prints to the screen, but does not edit the data
in the file.

The full PERL Script is just below.  If someone could have look and email me, I will hack through
it.  If it looks okay, it could be a server side permissions problem with my provider.

Thanks for your help.

Arthur Rosenfield

#! /bin/perl  -pi.bak
#
print "Content-type: text/html\n\n";

#The entry -pi.bak should tell the program to read the testfile into
#testfile.bak, open the original file testfile for writing, and edit
#testfile by substituting new information for old information
#based on the matched pattern.

#This first segment brings in an old name and a
#new name as data from a form that we'll call CHANGE FORM
#Pretty standard.

if ($ENV{'REQUEST_METHOD'} eq 'POST')
        {
        read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});


                {
                ($name, $value) = split(/=/, $pair);
                $value =~ tr/+/ /;
                $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
                $contents{$name} = $value;
                }
        }

#change the data from CHANGE FORM to simpler scalar
$oldfirst=$contents{'oldfirst'};
$newfirst=$contents{'newfirst'};

#open the data file and give it a filehandle
open (TESTFILE, "html/data/testfile");

#loop through the datafields
while (<TESTFILE>){               #reads each line from MYFILE into $_ till EOF

        #match pattern to restrict data edit to lines
        #begining with the word firstname:
        if ( /^firstname:/){    #is there an : sign in this line?

                #Split the line at the e:, throwing away the initial part
                #to isolate the data itself for patern matching AND EDITITING
                ($trash_it, $firstname) = split (/e:/);

                #since the data came from a file, chop the newline character
                chop ($firstname = $firstname);

                #make changes only if the firstname in TESTFILE matches
                #the name entered in the CHANGE FORM as oldfirst
                if ($firstname =~ /$oldfirst/) {

                #if a match, change firstname to the newfirst
                #from the CHANGE FORM
                $firstname =~ s/$oldfirst/$newfirst/;}

                #edit the original file represented by TESTFILE
                print ($firstname);                    
        }

Quote:
}              

close (TESTFILE);

exit 0;

There may be a better way to do this using an %array, but I am already on this course and need
to confirm that I can, indeed, edit data in a file.  Incidently. There is no problem attaching data to
datafiles using >> when opening the file.

Thanks for your help.



Mon, 15 Jun 1998 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Edit Data in a Datafile

2. Seeking advice on semantics (pros / cons and best ways of) using constants across modules

3. Better ways to deal with yppasswd?

4. Better ways to do this?

5. Better ways to...

6. Any better ways of doing this ?

7. Better Perl editing mode in Ema

8. I am a biginer i need to find a good tutorial in cfg

9. How can I 'edit' a datafile?

10. Why am I not picking up anything from a datafile?

11. some advice sought concerning a datafile read by a perl 4.036 program

12. Reproduce datafile

 

 
Powered by phpBB® Forum Software