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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



Maebius
Novice

Sep 6, 2000, 11:29 AM

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

Hello,
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]
NAME=Guest
EMAIL=something@mydomain.com
FULLNAME=Guestus Visitorus
BADGES=0
...other variables...
[end guest]
[begin maebius]
NAME=Maebius
EMAIL=maebius@dreamscape.com
FULLNAME=Maebius the Confused
BADGES=3
...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.
Thanks
-Maebius
maebius@dreamscape.com

------------------
Maebius
Living life -umop apisdn-
maebius@dreamscape.com


fashimpaur
User / Moderator

Sep 7, 2000, 4:33 AM

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

Maebius,

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\]/);
next;
}
else {
$playerEndFlag = 1 if (/\[end $login\]/);
if ($playerEndFlag){
print DAT "[end $login]\n";
$playerStartFlag = 0;
$playerEndFlag = 0;
$playerUpdated = 1;
next;
}
else {
$lineStat = (split(/=/, "$_"))[0];
print DAT "$lineStat=$STATS{$lineStat}\n";
}
}
}

close (DAT);
close (BAK);

</pre><HR></BLOCKQUOTE>

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

HTH,

Dennis


Maebius
Novice

Sep 7, 2000, 6:26 AM

Post #3 of 3 (291 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]
^NAME=maebius
^VAR1=something
^VAR2=something
^[end Maebius]
^[begin guest]
^^NAME=
^^VAR1=
^^VAR2=
^^[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!



------------------
Maebius
Living life -umop apisdn-
maebius@dreamscape.com

 
 


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

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