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

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion
Aleron
Wanderer


Joined: 01 Aug 2005
Posts: 76

PostPosted: Mon Mar 12, 2007 6:24 pm   

Idea: Key/Value Lists
 
I'm sure this might have been suggested a few times already, but since I'm not aware of it I'll go ahead. I'm not sure about anyone else, but I would find it useful if CMUD provided a map/hash variable type. There are some situations I've come across where this is a simpler/more elegant solution to a problem, but do not have any kind of mapping type available. Currently, I just use two list variables to accomplish it in this fashion:

Code:
%item(@listOfValues, %ismember(@someKeyName, @listOfKeys))


This works of course, but just isn't as flexible as a real map/hash type (where I can dynamically insert/remove from like a list) would be. Clearly there are some performance issues with map types since every time you read from them it's an O(N) search (unless you use hashes, but I doubt that'd be necessary for something like MUD scripts), but I don't think it would be anything to be concerned about, especially if it's clearly noted it's slower than an indexed list data type.

I'm not sure how useful this would be to anyone else. Perhaps some feedback in that regard from the community would reveal that.
Reply with quote
MattLofton
GURU


Joined: 23 Dec 2000
Posts: 4834
Location: USA

PostPosted: Mon Mar 12, 2007 8:36 pm   
 
Perhaps you should look at datarecord variables. Each one has a key field and a value field, like so:

Somelist.key = value

They work very similarly to how list variables work, and "value" can also in fact include stringlists. And, while doing so would make it much harder to work with, "key" can also be an entire phrase or sentence or returned result of a function instead of just a simple word.
_________________
EDIT: I didn't like my old signature
Reply with quote
vey2000
Novice


Joined: 21 May 2004
Posts: 32

PostPosted: Tue Mar 13, 2007 4:50 am   
 
This title made me think of something that I have been wishing there was: functions that return a list of keys or values from a db variable. As it stands I generally have to maintain separate lists for whichever ones I need, and that forces you to keep track of several variables whenever you do anything.

I know it should be possible to write a function to do something like this. Perhaps %subregex(%expanddb(@dbvar, "|"), "=[^|]+", "") might do, but only if the values aren't themselves lists and then things start to become complicated just to do something that should be basic. Or maybe I've just been spoiled by Python.
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Tue Mar 13, 2007 5:01 am   
 
I'm not sure if a list of values would be all that useful, partly because it'd be trivial to write your own function to do it for you if you really needed to once you have a function to return a list of keys, and because there aren't all that many situations where you'd need to have just the values if you're not using a stringlist.

A list of keys would be immensely useful, though.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Arminas
Wizard


Joined: 11 Jul 2002
Posts: 1265
Location: USA

PostPosted: Tue Mar 13, 2007 3:13 pm   
 
I usually end up with two database variables with string lists within them.
Example.
Var1 has ingame numbers as the key and Attributes as a string list.

platemail1234=Dragon scale|Defense 5000|state of repair 40000|Value 1000000 coins
PlateLegs3421=Dragon leggings|Defense 2000|state of repair 20000|Value 100000 coins
... more entries

And var2 would have a reference back.
Dragon leggings=platelegs3421|... more keys of dragon scale
Dragon scale=platemail1234|... more keys of dragon leggings

Then I write functions to go back and reference the information I need.
_________________
Arminas, The Invisible horseman
Windows 7 Pro 32 bit
AMD 64 X2 2.51 Dual Core, 2 GB of Ram
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23379
Location: Colorado, USA

PostPosted: Tue Mar 13, 2007 4:07 pm   
 
Some sort of %keys function is a good idea. I'll add that to the wish list.
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD 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