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
Fuego Ledrey
Wanderer


Joined: 09 May 2007
Posts: 64
Location: Dustin Acres, California

PostPosted: Mon Jun 25, 2007 8:08 pm   

Displaying keys in a database.
 
I am probably just not opening my eyes wide enough, but is there a function to get the # key in a database? Like %item, except for databases. This is how I'm doing it now, but I was still wondering.

Code:
#VAR afflictions    {}           {}
#addkey afflictions {stupidity}  {1}
#addkey afflictions {aeon}       {1}
#addkey afflictions {slickness}  {1}
#addkey afflictions {asthma}     {1}

#say %leftback(%item(%expanddb(@afflictions,"|"),1),2)", "%leftback(%item(%expanddb(@afflictions,"|"),2),2)", "%leftback(%item(%expanddb(@afflictions, "|"),3),2)", "%leftback(%item(%expanddb(@afflictions,"|"),4),2)


EDIT: No need to close code twice.
Reply with quote
Thinjon100
Apprentice


Joined: 12 Jul 2004
Posts: 190
Location: Canada

PostPosted: Mon Jun 25, 2007 8:54 pm   
 
I'm not sure what you mean here... are you referring to %db?
You'd use it like this...
Code:

#VAR afflictions    {}           {}
#addkey afflictions {stupidity}  {1}
#addkey afflictions {aeon}       {1}
#addkey afflictions {slickness}  {1}
#addkey afflictions {asthma}     {1}

#SAY %db(@afflictions,stupidity)
#SAY %db(@afflictions.aeon)


Please reply if that's not what you're looking for
_________________
If you're ever around Aardwolf, I'm that invisible guy you can never see. Wizi ftw! :)
Reply with quote
Fuego Ledrey
Wanderer


Joined: 09 May 2007
Posts: 64
Location: Dustin Acres, California

PostPosted: Mon Jun 25, 2007 8:57 pm   
 
No no, I need the FIRST one, no matter what it is, stupidity or aeon. The addkey's are just for example purposes. There's actually triggers and such that addkey and delkey from the variable afflictions.
_________________
EDIT: Image moved to Avatar FINALLY.
Reply with quote
Thinjon100
Apprentice


Joined: 12 Jul 2004
Posts: 190
Location: Canada

PostPosted: Mon Jun 25, 2007 8:58 pm   
 
Well, you /could/ use #LOOBDB and then #ABORT after the first run... off the top of my head I can't think of a parallel for that, but I'll look.
_________________
If you're ever around Aardwolf, I'm that invisible guy you can never see. Wizi ftw! :)
Reply with quote
Fuego Ledrey
Wanderer


Joined: 09 May 2007
Posts: 64
Location: Dustin Acres, California

PostPosted: Mon Jun 25, 2007 9:03 pm   
 
I was really looking for a function, rather than a command, but thanks, I hadn't thought of that.
_________________
EDIT: Image moved to Avatar FINALLY.
Reply with quote
nexela
Wizard


Joined: 15 Jan 2002
Posts: 1644
Location: USA

PostPosted: Tue Jun 26, 2007 1:21 am   
 
Either of these will work for the first one in the list

#say %db(@test,%word(@test,1,%char(30)))
#SAY %db(@test,%item(%expanddb(@test,"|","|"),1))
_________________
Zmud Support Library
Zmud Knowledge Base
Reply with quote
Fuego Ledrey
Wanderer


Joined: 09 May 2007
Posts: 64
Location: Dustin Acres, California

PostPosted: Tue Jun 26, 2007 1:56 am   
 
Those both seem to be echoing the VALUES, not the KEYS, which is what I need. Thanks for the input though.

On another note, what if I wanted the 3rd, 60th?... But that's not what I'm looking for anyway, heh.
_________________
EDIT: Image moved to Avatar FINALLY.
Reply with quote
nexela
Wizard


Joined: 15 Jan 2002
Posts: 1644
Location: USA

PostPosted: Tue Jun 26, 2007 2:41 am   
 
LOL Sorry Missed that part but I have re-written this to be all awesomeness where you can choose which index you want to use 1st 3rd 60th etc

#SAY %item(%expanddb(@test,"|","|"),1)

Even Better would be to make this a function (which I did and I know I will find a use for it :P)

Code:

***ZMUD + CMUD COMPATIBLE****Untested in Zmud Though
First paramater is the name of the datarecord including the @, Second paramater is the index number
#FUNC GetKeyByIndex {%item(%expanddb(%1,"|","|"),%if(%2>1,%eval(%2+%2-1),1))}
#FUNC GetValueByIndex {%db(%1,@GetKeyByIndex(%1,%2))}

***CMUD ONLY****
#FUNC GetKeyByIndex($DataRecord,$I) {%item(%expanddb($DataRecord,"|","|"),%if($I>1,%eval($I*2-1),1))}
#FUNC GetValueByIndex($DataRecord,$I) {%db($DataRecord,@GetKeyByIndex($DataRecord,$I))}



#LOOP 1,50 {#ADDKEY test Key%i Value%i}
To Get the key name for the third item
#SAY @GetKeyByIndex(@test,3)
Displays Key3

To get the value of the third key
#SAY @GetValueByIndex(@test,3)
Dispalys Value3
_________________
Zmud Support Library
Zmud Knowledge Base
Reply with quote
Fuego Ledrey
Wanderer


Joined: 09 May 2007
Posts: 64
Location: Dustin Acres, California

PostPosted: Tue Jun 26, 2007 3:44 am   
 
That is exactly what I wanted. Now, what if I wanted to display only the keys in a database?

I'll work on it, I'm sure it would have something to do with checking the database length and using your function. If you get something worked out before me, post it!

NOTE: I don't think I put enough emphasis on how kick BUTT those functions are!!

EDIT: Both functions are now equally appreciated, hehe.


Last edited by Fuego Ledrey on Tue Jun 26, 2007 4:14 am; edited 1 time in total
Reply with quote
nexela
Wizard


Joined: 15 Jan 2002
Posts: 1644
Location: USA

PostPosted: Tue Jun 26, 2007 4:03 am   
 
Retrieving just the key names is easy

#LOOPDB DataRecord {#SAY %key}

or
#LOCAL $Keys
#LOOPDB DataRecord {#ADDITEM $Keys %key}
#SAY $Keys

This will be 100* faster then trying to use my function to loop through everything :p
_________________
Zmud Support Library
Zmud Knowledge Base
Reply with quote
Fuego Ledrey
Wanderer


Joined: 09 May 2007
Posts: 64
Location: Dustin Acres, California

PostPosted: Tue Jun 26, 2007 4:16 am   
 
Oh oh, I forgot I didn't explain that enough, I want them to display as such:

Quote:
Stupidity, Aeon, Slickness, Asthma
Reply with quote
Fuego Ledrey
Wanderer


Joined: 09 May 2007
Posts: 64
Location: Dustin Acres, California

PostPosted: Tue Jun 26, 2007 4:21 am   
 
Bah, nevermind, too easy, heh.

Code:
#LOCAL $Keys
#LOOPDB @afflictions {#ADDITEM $Keys %key}
#SAY Afflictions: %expandlist($Keys,", ")


Seems to give me exactly what I wanted, heh.
_________________
EDIT: Image moved to Avatar FINALLY.
Reply with quote
Fang Xianfu
GURU


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

PostPosted: Tue Jun 26, 2007 9:39 am   
 
Unless there's some specific reason why you're using keys of a dbvar rather than a stringlist, I strongly recommend using the latter. At the moment, dbvars are no faster than string lists - both are stored in the same way, and dbvar lookups don't use hashes like you'd expect. This may change in the future, but right now using a dbvar rather than a string list is just creating problems like this one for you.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Dharkael
Enchanter


Joined: 05 Mar 2003
Posts: 593
Location: Canada

PostPosted: Tue Jun 26, 2007 7:32 pm   
 
I came up with a method for gathering the keys that, for very large dbvars anyways, seems to be faster than using #loopdb
Code:
#var keys %subregex(@DBRecord,"\x1E(?:[^\x1D]*?(?:\x1D|$))?","|")

I really don't know if it's faster or slower for small database variables but for a dbvar I made with over 2500 records it was quite noticeably faster than using #loopdb
I tried it with both alphanumerically sorted keys and randomized keys.
_________________
-Dharkael-
"No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style."
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