Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » zMUD General Discussion
nutsnbolts
Apprentice


Joined: 01 May 2002
Posts: 188
Location: USA

PostPosted: Tue Jun 11, 2002 8:26 pm   

Write to file duplication detection etc...
 
Ok, hopefully I didn't confuse anyone with the title but here is exactly what I want to do.

I have a trigger that captures players in-game through an "identify mob" spell.

This then allows me to either:

#alias reportmob {say @personname is @personlevel}
or
#write the @personame is @personlevel into an external file through aliases and triggers. (used later on for searching and displaying players at a later date)

Simple...

Now what I need is to enhance the #write ability to determin whether a person has been identified already. I'm not gonna go around identify'ng every person to update them and the mobs I need to only do once. But if for some odd reason or another, I forgot that I have identified this person already, I just dont' want to append a duplicate of that person. into the file.

This seems like a simple script and basically I just want to search the @playername to see if it's in there already and if it's not, then write. If it is, ignore it. Hell, if you are a powerful scripter, please maybe even prompt me to overwrite? This way I can truly update the database.

Overall, a quick summary... check the file if the playername already exists and if it does, ignore (or prompt for overwrite). If not, just append.

Thanks





Thank you for everything, it's always appreciated.
Reply with quote
TonDiening
GURU


Joined: 26 Jul 2001
Posts: 1958
Location: Canada

PostPosted: Tue Jun 11, 2002 10:47 pm   
 
Use

grep
quote:

Syntax: %grep(i,s)

search the ith file and return lines that match the pattern in s.
Example:

#FILE 1 "num_test.txt"

#99 {#WRITE 1 %i}
#CLOSE 1

this just creates a test file containing the numbers 1 to 99 (each on a line)

now:

#FILE 1 "num_test.txt"

#SHOW %grep(1,"[35]$")
#CLOSE 1

displays a list of all lines ending in either a 3 or 5; i.e. it outputs:

Opened num_test.txt as file 1

3|5|13|15|23|25|33|35|43|45|53|55|63|65|73|75|83|85|93|95
Closed num_test.txt on file 1


to figure out if you've already recorded information about someone.

Ton Diening
Providing untested answers that tend to be
more complicated than others.
Reply with quote
nutsnbolts
Apprentice


Joined: 01 May 2002
Posts: 188
Location: USA

PostPosted: Wed Jun 12, 2002 1:12 am   
 
Anyone want to take a crack at how to use the #if statements in conjunction with grep function?

Thank you for everything, it's always appreciated.
Reply with quote
nutsnbolts
Apprentice


Joined: 01 May 2002
Posts: 188
Location: USA

PostPosted: Wed Jun 12, 2002 1:42 am   
 
The ideal script is something where, I have an alias to writemob after I have identified the item. I created a reportmob alias that grabs the information from the identify spell and places it in a say format on one line.

Anyway, after doing the alias writemob, this will then search the file for a match, if it finds one, it bring it up in a #show and ask if I want to overwrite. Obviously, the reportmob I will incorporate within it so I can see the comparison whether I want to overwrite or not.

That's basically it. Currently, I have a writemob that opens the file and writes into the file directly regardless of it being in there or not. I just want to get rid of thinking whether I have the ident in the file.

Thank you for everything, it's always appreciated.
Reply with quote
Kjata
GURU


Joined: 10 Oct 2000
Posts: 4379
Location: USA

PostPosted: Wed Jun 12, 2002 1:25 pm   
 
This is where text files show their weakness agaisnt db's. If you want to do this this way, you can prevent adding duplicates bu using %grep like Ton suggested. Example:
#IF (%numitems(%grep(1, @personname))) {#SAY @personname already has an entry} {#WRITE 1 {@personname is @personlevel}}

What is tedious is the overwriting. You could use a record file (file 6-10), but some quick testing revealed that they are really unreliable. So, using sequential text files, the only option is creating temporary files and copying line by line while replacing the necessary line. It would have to be something like this:
#FILE 2 temp.txt
#RESET 1
#LOOP %filesize(1) {#VAR line %read(1);#IF (@line =~ "@personname is %d") {#WRITE 2 {@personname is @personlevel}} {#WRITE 2 {@line}}}
#CLOSE 1
#ERASE database.txt
#FILE 1 database.txt
#RESET 2
#LOOP %filesize(2) {#WRITE 1 {%read(2)}}
#CLOSE 2
#ERASE temp.txt

This is all, of course, assuming again that the file you are talking about in this post is already open as file 1.

Kjata
Reply with quote
nutsnbolts
Apprentice


Joined: 01 May 2002
Posts: 188
Location: USA

PostPosted: Wed Jun 12, 2002 2:23 pm   
 
Kjata,

The first part using the grep worked wonders.

The second part of your script, I'm sure what exactly is doing? Are you saying that in order to replace the duplicate, it would have to create a temp file where it writes everything line by line until it hits the duplicate and then overwrites that duplicate, then continues writing the rest of file.

Questions is, how long does this take?
Lag time?

Thank you for everything, it's always appreciated.
Reply with quote
LightBulb
MASTER


Joined: 28 Nov 2000
Posts: 4817
Location: USA

PostPosted: Wed Jun 12, 2002 3:54 pm   
 
Yes, that's exactly what Kjata is saying. You can only write to the end of a sequential file, so the only way to replace something is to rewrite the entire file.

The problem with structured files (6-10) is that each record must be the same length. Shorter lines would be padded with nulls or spaces, and longer lines would have to be truncated. However, the #OPEN command doesn't provide any way to specify that length so you would have to use whatever length Zugg has set as a default. I'm unable to find the default length anywhere, but it could be determined fairly easily by experimentation.

As Kjata hinted, a database would be much more suitable for this task.

LightBulb
Vague questions get vague answers
Reply with quote
nutsnbolts
Apprentice


Joined: 01 May 2002
Posts: 188
Location: USA

PostPosted: Wed Jun 12, 2002 4:11 pm   
 
I see...

I initially started with the database route, however, this required more tasks than I have expected such as importing all the information into the database, outlining the fields in the database, etc....

By this route, I was able to just cut n' paste what was already available--placed it in a file, and query the file and output it in the mud.

That's when I came across the "what if's":
What if I had a mob/item already in the database?
Answer: it would just append the item, regardless to the end of the file
What if I want to update the database with correct stats?

Answer: I would then have to rewrite the file
or
Manually change it myself but also have a script to tell me if that specific item/mob is already in there when I try to do a write command.


I thought maybe I can get away with some script that allowed me to rewrite a specific line when it tells me that the item/mob is found in the database or even just delete that specific line and append the new identified mob/item.

Rewriting the file seems like a way to go but I was wondering how fast this would take. I guess it would depend on the file size of the txt. But it's not even that huge so I'm gonna see if it would work?

Thank you for everything, it's always appreciated.
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » zMUD General Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net