Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: flat file databases

by beable (Friar)
on Aug 12, 2004 at 05:48 UTC ( #382180=note: print w/replies, xml ) Need Help??

in reply to flat file databases

Hello Travis, I'd suggest you take a look at the Tie:File module, which lets you treat files as arrays. This would make it easy for you to change the data in your file, assuming the data were able to be dealt with in this way. Of course, I'm only guessing here, because I haven't seen your data. Here's a simple example of using Tie::File:
#!/usr/bin/perl use Tie::File; use strict; use warnings; my $filename = "dbfile.txt"; tie my @array, 'Tie::File', $filename or die "can't open $filename: $! +"; $array[13] = 'blah'; # line 13 of the file is now 'blah' print $array[42]; # display line 42 of the file __END__

Replies are listed 'Best First'.
Re^2: flat file databases
by Travis M. (Novice) on Aug 13, 2004 at 00:40 UTC
    Thank you for your help....I'm now in the process of looking at the tie module...My biggest problem is that each file is read into an array. I have to split each line of each array into yet another array, where I make a an update to one or two particular elements...That's where I get into trouble. I can't seem to make that change propagate back into the very first array (the one from which each line is made into a sub-array). Here's my code that I am working with. If you get overly bored, would you mind giving it a once-over? I am really at a loss here and have spent several days trying to make it work... 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 = $array[1]; $osuf = $array[2]; $update = $array[3]; #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 = $array2[1]; $osuf2 = $array2[2]; $last = $array2[3]; $curr = $array2[4]; #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 ch +ecking 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 an +d #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"; }

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (1)
As of 2023-03-21 04:44 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (59 votes). Check out past polls.