CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
Reading/replacing chunks of text in a flat-file DB



Sep 6, 2000, 11:29 AM

Post #1 of 3 (441 views)
Reading/replacing chunks of text in a flat-file DB Can't Post

I am writing a game script that uses a player.dat file to store all sorts of user information. The file is in the format of:

[begin guest]
FULLNAME=Guestus Visitorus
...other variables...
[end guest]
[begin maebius]
FULLNAME=Maebius the Confused
...other stuff...
[end maebius]

So far, I have been able to read in the player information as follows (The $login variable is recalled from a cookie earlier):

open (INF, 'player.dat');
@playerdata = <INF>;
close <INF>;
for $dataline(@playerdata) {
if ($dataline =~ m/\[begin $login\]/ .. $dataline =~ m/\[end $login\]/){
($key,$value) = split(/=/,$dataline);
$STATS{$key} = $value;


My question is what would be the best way to replace the player info into the database when it changes?
I've thought of reading the whole player.dat file into another @array and then replacing chunks with the (updated) %STATS hash with some funky IF statements, then just replacing the entire data file by writing the big @array back to player.dat
That seems a bit clumsy and not very efficient.

any suggestions would be GREATLY appreciated.

Living life -umop apisdn-

User / Moderator

Sep 7, 2000, 4:33 AM

Post #2 of 3 (441 views)
Re: Reading/replacing chunks of text in a flat-file DB [In reply to] Can't Post


Try something like this:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

rename ("player.dat", "player.bak") or die "Can't create backup file.";

open (BAK, "player.bak");
open (DAT, ">player.dat");

$playerStartFlag = 0;
$playerEndFlag = 0;
$playerUpdated = 0;

while (<BAK> ){
if (!$playerStartFlag){
print DAT "$_";
next if ($playerUpdated);
$playerStartFlag = 1 if (/\[begin $login\]/);
else {
$playerEndFlag = 1 if (/\[end $login\]/);
if ($playerEndFlag){
print DAT "[end $login]\n";
$playerStartFlag = 0;
$playerEndFlag = 0;
$playerUpdated = 1;
else {
$lineStat = (split(/=/, "$_"))[0];
print DAT "$lineStat=$STATS{$lineStat}\n";

close (DAT);
close (BAK);


Remember, you will need to provide some kind
of file locking if the game can be played by
more than one player simultaneously.




Sep 7, 2000, 6:26 AM

Post #3 of 3 (441 views)
Re: Reading/replacing chunks of text in a flat-file DB [In reply to] Can't Post

Thanks, fashimpaur,

This *mostly* works, however it puts a space at the beginning of each line. I've tried doing some basic RegEx to try to remove the initial whitespace, but it is not working too well.

After running your proposed code, the data file now looks like this... (spaces will be shown as ^ signs since there are none in the code. Wink (If I login as Guest.)

[begin Maebius]
^[end Maebius]
^[begin guest]
^^[end guest]

Also, since there are now whitespaces, the $STATS{$key} does not match. The $keys change from 'NAME' to ' NAME', and are thus blanked out.
If I would login as Maebius, the Guest info would remain (with the whitespace), but the section with Meabius Data would be lost as Guest was in my example above.

I'll try to work on this myself, but wanted to show what happens when the proposed code is run as-is.
Thanks though! It's a better start in the right direction!

Living life -umop apisdn-


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives