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
HariKari
Wanderer


Joined: 16 Feb 2001
Posts: 57

PostPosted: Mon Mar 03, 2003 6:50 am   

Removing items from a list
 
Is there a way to remove an item from a list if you don't know exactly what the item is?

Mm... usually I would create some sample triggers to explain on the forums, but I'll post part of my actual script this time.
(This is for Medievia)

Pattern:
A ({horse pulling a covered wagon|horse pulling an open wagon|large packhorse|pack mule}) can carry (%d) %w of (%w) packed with (%d) %w of (%w). We'll pay you (%n) gold for that.

Action:
vtValue = %concat( %format( "&10.0n", %6), " : %2 %3 and %4 %5")
#if (!%pos( %left( @vtValue, 15), @vtValues) & !%pos( %rightback( %left( @vtValue, 19), 6), @vtValues)) {
#additem vtValues {@vtValue}
vtValues = %sort( @vtValues, 1)
}
#add vtCount (-1)
#if (@vtCount == 0) {
vtFreight = %1
vtPrint
#T- vtGetData
}

Information:
You can have different sized freights that can carry varying number of items based on the item's size and weight.
The in game value command tells how many of a given item a freight can carry and packed with another item (if there is space).

Sample Output:
A horse pulling a covered wagon can carry 11 sacks of adamantite packed with 0 sacks of coal. We'll pay you 769,230 gold for that.
A horse pulling a covered wagon can carry 11 sacks of adamantite packed with 0 sacks of iron. We'll pay you 769,230 gold for that.
A horse pulling a covered wagon can carry 11 case of adamantite packed with 1 case of opium. We'll pay you 782,698 gold for that.
A horse pulling a covered wagon can carry 11 chests of adamantite packed with 0 chests of tools. We'll pay you 769,230 gold for that.
A horse pulling a covered wagon can carry 21 crates of coal packed with 0 crates of adamantite. We'll pay you 685,020 gold for that.

Examination:
!%pos( %left( @vtValue, 15), @vtValues) checks if the price and number of a certain item is duplicate (this gets rid of all those values that are "11 adamantite and 0 *"
"...769,230 : 11" is what's matched with the left 15 (... is for the spaces)
This part works fine and isn't really the problem... this next thing is
!%pos( %rightback( %left( @vtValue, 19), 6), @vtValues) is supposed to make sure there are no duplicates with the same number of items. Originally I put this in so it would save "11 adamantite and 1 opium" and junk the other ones that are "11 adamantite and 0 blah"
"11 ada" is what gets checked

Problem:
For my sample output, the adamantite with opium is valued near the middle. It gets rejected because "11 ada" is already in @vtValues. But I want it to be added to the list of values because it's price is better than packing adamantite with nothing.

Possible solution:
Make the second condition of the if statement an if statement for itself inside of the true clause of the first if. If the second condition is true, remove the item with ("11 ada") from the list and add in the new value.
This is what brings me to this message post. How do I remove that item with "11 ada" ?

Thanks
-HariKari
Reply with quote
Troubadour
GURU


Joined: 14 Oct 2000
Posts: 556
Location: USA

PostPosted: Mon Mar 03, 2003 10:17 am   
 
I understand your problem as being you wish to create a list of trades comprising the best compensation involving a primary good regardless of what the secondary good may be. In the examples you gave you might end up with a list of two trades: one of 11 adamantite and 1 case of opium for 783k, and one of 21 crates of coal and 0 crates of adamantite for 682k; you wish to ignore all of the less profitable adamantite trades.

I recommend that when you collect your data you store it to a db variable. You can then cycle through the list of all previous trades to see if any of the primary goods are duplicated and if so, keep the one giving the higher compensation. If no match on the primary good is found, the information is added to list of all trades (since it's a new primary good.)

Keeping your pattern, your new action would be:

trade.primary = %2
trade.priquan = %1
trade.secondary = %4
trade.secquan = %3
trade.compensation = %5
match = 0
#FORALL @alltrades {#IF ((%i.primary = @trade.primary) & (@trade.compensation > %i.compensation)) {#DELITEM alltrades %i; #ADDITEM alltrades @trade; match = 1} {#NOOP}}
#IF !@match {#ADDITEM alltrades @trade} {#NOOP}

To subsequently manipulate the data, you'll have to extract each list item to a db variable.

Troubadour
(Win 98, Pentium III, 550 MHz)
Reply with quote
HariKari
Wanderer


Joined: 16 Feb 2001
Posts: 57

PostPosted: Tue Mar 04, 2003 5:30 am   
 
Oooh, that looks good.

I haven't used dbs in zMUD before. I guess I'll look into those now, but it does seem straightforward.

Kinda like arrays/vectors in some other programming languages. blah["name"] => blah.name

Thanks,
HariKari
Reply with quote
HariKari
Wanderer


Joined: 16 Feb 2001
Posts: 57

PostPosted: Tue Mar 11, 2003 1:00 am   
 
Btw.. what do you call these "variable databases"?
I was looking through the help files, but it seems like databases are something else.
I've had some database experience with MySQL, and it seems like there is support for that kind of database with records and keys.

So yes.. what do you call these "autodatabase variables" and what help files can I look at?
Reply with quote
Kjata
GURU


Joined: 10 Oct 2000
Posts: 4379
Location: USA

PostPosted: Tue Mar 11, 2003 1:41 am   
 
They are called record variables or database variables.

Kjata
Reply with quote
HariKari
Wanderer


Joined: 16 Feb 2001
Posts: 57

PostPosted: Tue Mar 11, 2003 2:35 am   
 
Is there any easy way to sort a list of record variables?
SELECT * FROM vtValues ORDER BY value DESC

Oh hrmm.. that's kinda interesting ;) MySQL with zMUD!
Reply with quote
Kjata
GURU


Joined: 10 Oct 2000
Posts: 4379
Location: USA

PostPosted: Tue Mar 11, 2003 4:29 am   
 
No, no easy way. Although they are called record variables, they are more like hash tables.

Kjata
Reply with quote
HariKari
Wanderer


Joined: 16 Feb 2001
Posts: 57

PostPosted: Tue Mar 11, 2003 4:43 am   
 
So the database can't sort either or can it?

But one way to sort would be with what I was doing earlier.
#forall @vtValues {
vtValue = %concat( %format( "&10.0n", %i.value), " : %i.goodNum %i.good and %i.packNum %i.pack")
#additem vtValuePrint {@vtValue}
}
vtValuePrint = %sort( @vtValuePrint, 1)
Reply with quote
Kjata
GURU


Joined: 10 Oct 2000
Posts: 4379
Location: USA

PostPosted: Tue Mar 11, 2003 1:54 pm   
 
Well, normally what I do is fool zMUD into thinking that the record variable is a stringlist. Here is an example that sorts by keys (the simplest method):
#VAR rec ""
#ADDKEY rec key2 test2
#ADDKEY rec key3 test3
#ADDKEY rec key1 test1
#VAR rec %replace(@rec, %char(29), "|")
#VAR rec %sort(@rec)
#VAR rec %replace(@rec, "|", %char(29))

You can also sort by values (which is more complicated):
#VAR rec ""
#ADDKEY rec key2 test2
#ADDKEY rec key3 test3
#ADDKEY rec key1 test1
#VAR temp ""
#LOOPDB @rec {#VAR temp %concat(@temp, "|", %val, "=", %key)}
#VAR temp %delete(@temp, 1, 1)
#VAR temp %sort(@temp)
#VAR rec ""
#FORALL @temp {#ADDKEY rec %item(%replace(%i, "=", "|"), 2) %item(%replace(%i, "=", "|"), 1)}

Kjata
Reply with quote
HariKari
Wanderer


Joined: 16 Feb 2001
Posts: 57

PostPosted: Wed Mar 12, 2003 6:46 am   
 
Oh, and for anyone that has a similar situation to this.. the solution Troubadour was almost correct for my situation.

vtValue.goodNum = %2
vtValue.good = %3
vtValue.packNum = %4
vtValue.pack = %5
vtValue.value = %6
vtMatch = 0
#forall {@vtValues} {
..#if (%i.good = @vtValue.good) {
....#if (%i.value < @vtValue.value) {
......#delitem vtValues %i
......#additem vtValues @vtValue
......}
....vtMatch = 1
....}
..}
#if (!@vtMatch) {#additem vtValues @vtValue}

This will make it so there will only be one of a given main good.
The original script would still add the values that are less than the best value for a given good.

Thanks again for pointing me towards the database variables.

HariKari

(Oh.. btw.. is there a better way of doing something like this?
%eval( @%concat( "vt", %i.good).post)
This is within a #forall on @vtValues which contains the best values for the goods.
It first gets %i.good to get something like.. "arrows"
I then concat a "vt" because I like to name all my variables with a prefix of the class.. such as vt.
So the variable is actually @vtarrows which is another database variable.. with one of the records being "post" (the 'city' that the good belongs to))
Reply with quote
Kjata
GURU


Joined: 10 Oct 2000
Posts: 4379
Location: USA

PostPosted: Wed Mar 12, 2003 1:38 pm   
 
It's not prettier, but the correct way would be:
%db( @{%concat( "vt", %db( @{%i}, good))}, post)

Kjata
Reply with quote
HariKari
Wanderer


Joined: 16 Feb 2001
Posts: 57

PostPosted: Thu Mar 13, 2003 5:49 am   
 
Btw.. why is it @{%i} ?
Wouldn't that look for a variable with the name of whatever is stored in %i?
Reply with quote
Kjata
GURU


Joined: 10 Oct 2000
Posts: 4379
Location: USA

PostPosted: Thu Mar 13, 2003 12:43 pm   
 
Yes, it would do that. This is assuming that @vtValues is a list of names of record variables. If @vtValues is a list of record variables (instead of the names), then do remove the @ and leave only the %i.

Kjata
Reply with quote
HariKari
Wanderer


Joined: 16 Feb 2001
Posts: 57

PostPosted: Sat Mar 15, 2003 2:17 am   
 
If the correct way to access a value from a database is %db(variable, key).. is the correct way to add/modify a key value is #addkey instead of variable.key = <value>

Also, how would someone be able to post a script onto the forums that use database variables?

Hrmm.. I'll try right here. ;)

#VAR vtarrows {postAthelaseacovered47wagon37mule9pack6}
Reply with quote
Kjata
GURU


Joined: 10 Oct 2000
Posts: 4379
Location: USA

PostPosted: Sat Mar 15, 2003 10:56 am   
 
Well, it's not the absolute correct way, but in the way you were using it, it was. The thing is that the . syntax doesn't allow for much flexibility (like easily building the key name on the fly), while with %db you can do this more clearly and error-free. To modify a key's value, you could use either method, but again, you have to consider that the command is more flexible than the . syntax.

I guess you could post the variable like that and zMUD should read it fine. However, what most of us do is to have the script create the variable using either #ADDKEY or the . syntax, or just tell the user of the script how to initially build the variable themselves.

Kjata
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