Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: flat file databases

by Zero_Flop (Pilgrim)
on Aug 12, 2004 at 05:38 UTC ( #382177=note: print w/replies, xml ) Need Help??


in reply to flat file databases

Travis-

How big are these files?

Does the first file only contain one record, or multiple records?
If it has only one record I would try doing this as follows:

Open and read the first file.
Loop though the second file reading one line at a time.
Read the line, compare to the record you want to change to the record you just read, then write to a temp file the correct entry.
Once you have gone though the entire second file. You will have two files, the original second file and a temp file that is identical to the second file but with the record change. If your record change was successful then rename the old second file to something like data.old and rename the temp file to the name of the second file. ( this give you a roll back ability if something goes wrong) You can have multiple data.old files to act as an undo feature.

If you have multiple lines you have to replace, I would consider using a module on CPAN that would give you a generic interface to your data.
Good luck
ZeroFlop

P.S. You may just be off a little in your code. Posting the reivant part will help in supporting you!

Replies are listed 'Best First'.
Re^2: flat file databases
by Travis M. (Novice) on Aug 13, 2004 at 00:31 UTC
    Thanks for the advice, but I am still having problems. I have attached the code I have thus far written (knowing full well it could probably be more concise) as well as some sample data....If you have nothing better to do, would you mind giving it a once over? Perhaps you can provide some additional suggestions... Thanks again, Travis
    #!C:/Perl/bin/perl -w
    #use CGI ':standard';
    open (UPDATE, "update.txt") or die "Cannot open update.txt";
    @data = <UPDATE>;
    close (UPDATE);
    #SAMPLE DATA FROM THIS ARRAY IS AS FOLLOWS:
    #T00001 0123-12345 DD001 67
    #T00002 0123-12345 DD001 99
    #T00003 0123-12345 DD002 0
    #T00008 0123-12346 DD001 76
    #T00014 7777-77777 DD001 88
    #T00020 0999-99999 DD001 99
    open (MDF, "mdfl.txt") or die "Cannot open update.txt";
    @data2 = <MDF>;
    @data2 = @data2;
    close (MDF);
    #SAMPLE DATA FROM THIS ARRAY IS AS FOLLOWS:
    #T00001 0123-12345 DD001 100 100
    #T00002 0123-12345 DD001 100 100
    #T00003 0123-12345 DD001 100 100
    #T00004 0123-12345 DD001 100 100
    #T00007 0123-12345 DD002 100 100
    #T00008 0123-12346 DD001 100 100
    #T00013 7777-77777 DD002 100 100
    #T00014 7777-77777 DD002 100 100
    #T00015 7777-77777 DD003 100 100
    #T00016 8888-88888 DD001 100 100
    #T00019 8888-88888 DD003 100 100
    #T00020 9999-99999 DD004 100 100
    #I start by reading each line fo the first array, called @data
    #I split this line on tabs
    foreach $line (@data) {
    chomp $line;
    @array = split (/\t/, $line);
    $dmpiid = $array[0];
    $be = $array1;
    $osuf = $array2;
    $update = $array3;
    #Then I read each line from the second array, splitting it on tabs too
    foreach $line2 (@data2)
    {
    chomp $line2;
    @array2 = split (/\t/, $line2);
    $dmpiid2 = $array2[0];
    $be2 = $array21;
    $osuf2 = $array22;
    $last = $array23;
    $curr = $array24;
    #next I find where certain values are the same from each array
    if (($dmpiid) eq ($dmpiid2)) {
    #when found, I update the appropriate values as indicated
    $last = $curr;
    $curr = $update;
    #next, I push the following string back into the @data2 array
    $new = "$dmpiid2\t$be2\t$osuf2\t$last\t$curr\n";
    push (@data2, $new);
    #next I jump out of the inner loop because there's no need to keep checking
    last;
    #however, once I do that, I need to delete the previous instance of
    #the data I just updated, otherwise the array will contain both old and
    #new data...this is where I get stuck
    } #end if
    } #end inner foreach
    }# end outer foreach
    #this is my test which prints my results. unfortunately, the old data is
    present allong with the new data.
    foreach $item (@data2) {
    print "$item\n";
    }
      This may not be a popular solution, but if your files are a decent size you could use a hash.

      Read each line of the data file and split on the first tab. Turn this into a hash with the ID as the key and the remaining of the line as the data for the hash. (You could also use a hash of arrays)

      Now you can load the update file, line by line.
      Turn the line into an array.
      Use the first array entry as the hash key to get the hash data.
      Create the new hash data string from the update data.
      Update the Data Hash.
      Read next line from update file.

      You end up with an updated hash. Now print that back to the file.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://382177]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2022-10-05 10:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (22 votes). Check out past polls.

    Notices?