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
Anaristos
Sorcerer


Joined: 17 Jul 2007
Posts: 821
Location: California

PostPosted: Tue Jun 17, 2008 12:12 am   

A far out idea to be added to the wishlist - Corresponding stringlists.
 
Let's say that I have two string lists which have related values:
Code:

$sl1 = "1|3|7|9|2"
$sl2 = "v1|v3|v7|v9|v2"

I would like a way to update one string list correspondingly when the other string list is sorted. So that
Code:

$sl1 = %sort( $sl1 )

Would force
Code:

$sl2 = "v1|v2|v3|v7|v9"

There is no easy way to do this in script, though it can be done. Note that v1,v2,...v9 are not the actual values, but a representation of values.
One other way to do this without correspondence would be to allow one to tell %sort how to do its job, in which case pseudo-db variables could be used inside the a string list to do this.
So %sort(listname[,sortfunction]) would work just fine for the scheme. Where sortfunction would be a standard user-defined CMUD function which would take the current item as parameter.
_________________
Sic itur ad astra.
Reply with quote
Fang Xianfu
GURU


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

PostPosted: Tue Jun 17, 2008 12:18 am   
 
Use a dbvar to store the values. So, you have something like:

#function AddItemToMyList {#additem $sl1 %1;#addkey $sl2 %1 %2}

Now, you sort the list with %sort($sl1) like you always did, and when you want the value, you do %db($sl2,"keyname")
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Anaristos
Sorcerer


Joined: 17 Jul 2007
Posts: 821
Location: California

PostPosted: Tue Jun 17, 2008 12:34 am   
 
Yes, Fang, that's the way I do it now. The problem with using dbvars is, of course, the hashing. Which comes into play if the dbvar is a complex one. From what I can tell, %sort actually will sort on the first keyname returned by the hash, when, in fact, I am looking to sort the key values of a particular key. So I would like a way to sort the list my way. It is not uncommon for sort functions to allow the user to determine the sort scheme.

EDIT: adding a sortfunction to %sort would basically turn it into a selection function rather than just a sorting one.
_________________
Sic itur ad astra.
Reply with quote
Fang Xianfu
GURU


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

PostPosted: Tue Jun 17, 2008 12:57 am   
 
I think you need to be more specific in what you want to do.

In your original post, you say you want to be able to sort the list $sl1 and have the list $sl2 (which holds values associated with the items in $sl1) updated to reflect the new order, presumably so that if you refer to items in $sl2 by the %ismember return from $sl1, you'll get the correct item. If you use a dbvar, the order of $sl2 doesn't matter any more. This is usually what people are after when they encounter problems with lists like this.

But now you're talking about specifying the sort scheme and sorting (I think) by the values rather than the keys, which would look something like this in lua:

Code:
table.sort(table,function (a,b) return table[a]>table[b] end)


So is that what you're after, or what?
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Anaristos
Sorcerer


Joined: 17 Jul 2007
Posts: 821
Location: California

PostPosted: Tue Jun 17, 2008 5:22 am   
 
Actually, both. But by specifying a sort function to %sort, it wouldn't matter what $sl2 contained (or $sl1, for that matter). The point is to retrieve the values of a ([n] associated) list without resorting to the cumbersome scheme of putting the values in a dbvar. Sorting the list may not yield the right order because the %sort function doesn't "know" that they are dbvars. What it will do to the list is sort it lexically, which may not be what one would want.
_________________
Sic itur ad astra.
Reply with quote
Rahab
Wizard


Joined: 22 Mar 2007
Posts: 2320

PostPosted: Tue Jun 17, 2008 2:26 pm   
 
Why do you need it sorted? Is there some activity you want to do that requires the keys to be sorted? Depending on what you want to do, it may be enough to use a dbvar and do a %sort() on %keys(). I don't see that using a dbvar is any more "cumbersome" than maintaining separate stringlists. Can you explain what you want to do with it?
Reply with quote
Anaristos
Sorcerer


Joined: 17 Jul 2007
Posts: 821
Location: California

PostPosted: Wed Jun 18, 2008 12:54 am   
 
Before I answer you, Rahab, let me say that most applications have features that I would not have thought of including but once I find that they are available I try to think of ways of using them. Another way of looking at it would be that not finding a use for a feature doesn't mean that the feature is not useful.
The problem I have is that data is coming in from different streams into these string lists, and they come in asynchronously. When it is time to correlate the data I have to keep track of what is which and make sure that what I have done to this I also have done to that, etc.
My point was to come up with a structure that was easier to manipulate than dbvars. These variables (dbvars) are used in CMUD in lieu of data structures that you find in more rigidly structured languages. As it turns out, my thoughts were running along the line of object-like structures for CMUD without the formality of methods (though that would have been nice).
As my title aptly stated, this was a far out idea. I put it out there as something to be considered. I can live without it.

EDIT: Here is what Fang said to shalimar in re sorting dbvars:
Fang Xianfu wrote:
There's no such thing as sorting a dbvar any more. They're stored as hashes. You'll need to find some other way of doing it, probably by building a stringlist whose items are the keys of the db, which you then loop through in order, accessing the dbvar with %db to get the number.

With corresponding string lists, this would be a piece of cake.
_________________
Sic itur ad astra.
Reply with quote
Rainchild
Wizard


Joined: 10 Oct 2000
Posts: 1551
Location: Australia

PostPosted: Wed Jun 18, 2008 2:05 am   
 
I agree that a "NameValueCollection" (in .NET speak) would be quite a useful addition. In the mean time, as a work around, you could create your own by storing both key and value in the same stringlist?

$sl = {1!v1|3!v3|7!v7|9!v9|2!v2}
#SHOW > %sort($sl)

returns what you would expect: 1!v1|2!v2|3!v3|7!v7|9!v9 ... you would have to use a %left / %right or similar to pull the key and value apart, but it should work fine?

You don't necessarily have to even have a "!" to separate the records, you could pad the key with spaces - for example make all keys 6 characters long which means you don't have to search for the "!" each time - you can just grab the 6 leftmost characters and know you have the key (%trim it if required).

Incidentally, I did try using "=" to separate key and value, but it gets messed up, hence why I went with "!" in the example.

Edit: If you're concerned about doing a %left/%right/%copy/etc all the time, you could maintain your 2 stringlists and only combine them when you want to do the sort, then uncombine them again back into your two lists.
Reply with quote
Fang Xianfu
GURU


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

PostPosted: Wed Jun 18, 2008 9:24 am   
 
You can use something more obscure to separate them - Viji uses the divide sign, which I have no idea how to reproduce here.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Taz
GURU


Joined: 28 Sep 2000
Posts: 1395
Location: United Kingdom

PostPosted: Wed Jun 18, 2008 10:53 am   
 
You should be able to use ampersand divide semicolon or ampersand hash 247 semicolon.

Testing: ÷ ÷

EDIT: I guess you can't use the former but the latter works.
_________________
Taz :)
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