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
Progonoi
Magician


Joined: 28 Jan 2007
Posts: 430

PostPosted: Tue Oct 21, 2008 8:43 pm   

Sublists within db var
 
Maybe not the best subject header but at least part of my question/problem.

Right now I'm effectively using scheme of

db key - subvalue=subvalue's value|etc

And then taking the data out and removing stuff which I don't need with %subregex etc.

However, I was wondering here if I'm on a right track.

I tried to use the scheme of

key - value

where value itself is secondary db var with key/value pairs and obviously it works, too and is easier to read, but.

The secondary db var *has to be* in the order I set it to be and with the hashing rules it's impossible to make true,
as it's random and sometimes keys are how you want them to be, other times they are not and you can't do a thing about it.

I bet someone would say Lua now but I'm someone who likes to fully write his own code, hence I wouldn't want to use someone elses
(Arminas', obviously), but I also don't have any time to start heavily learning Lua.

I'm open for suggestions but I kind of feel there aren't many on the table.

Thanks in advance.
_________________
The Proud new owner of CMud.

--------------------------------
Intel Core i5-650 3,2GHz
4 DD3 RAM
GTX 460 768MB
Win 7 Home Premium 64x
--------------------------------
Reply with quote
Arminas
Wizard


Joined: 11 Jul 2002
Posts: 1265
Location: USA

PostPosted: Wed Oct 22, 2008 1:35 am   
 
This one isn't as effective as the Lua version, (HashSet), but it is all Zscript. And I haven't gone out and updated the HashSet Function in a while anyway.
It allows you to write tables up to two levels deep. For tables only one level deep all you need is addSubkey and delSubkey.

If you need two levels deep then you would use addSubkey2

In my implementation I never need to just delete a second level subkey I would just delete the whole table beneath the first key so I never made a delSubKey2.

Anyway enjoy.

Code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<cmud>
  <class name="subKeys" copy="yes">
    <alias name="createGirl1" copy="yes">
      <value>#call @addSubKey(childrenV,girls,Jenna)
#call @addSubKey2(childrenV,girls,Jenna,hair,brown)
#call @addSubKey2(childrenV,girls,Jenna,eyes,blue)
#call @addSubKey2(childrenV,girls,Jenna,torso,"white blouse")
#call @addSubKey2(childrenV,girls,Jenna,legs,"blue skirt")
#call @addSubKey2(childrenV,girls,Jenna,feet,"ruby slippers")</value>
    </alias>
    <func name="addSubKey" copy="yes">
      <value>#Section AddSubKey {
#IF (%trigger(%quote($var))==-1) {#var %quote($var) {}}
#if (%vartype(%quote($var))!=5) {#call %vartype(%quote($var),5)}
#addkey $dbVar {%expanddb(@{$var}.$key,"|")}
#delkey $dbVar .$key
#addkey $dbVar $skey $val
#addkey %quote($var) $key {$dbVar}}</value>
      <arglist>$var,$key,$skey,$val</arglist>
    </func>
    <func name="addSubKey2" copy="yes">
      <value>#IF (%iskey(@{$var.$key},$skey)==0) {#call @addSubKey($var,$key,$skey,$skey1)}
#addkey $dbVar {%expanddb(@{$var.$key.$skey},"|")}
#delkey $dbVar $skey.$skey
#addkey $dbVar $skey1 $val
#call @addSubKey($var,$key,$skey,$dbVar)</value>
      <arglist>$var,$key,$skey,$skey1,$val</arglist>
    </func>
    <func name="CheckSubKey" copy="yes">
      <value>#loopdb @{%quote($var)} {#if (%iskey(%val,$val)) {#Return {%key}}}
#Return {0}</value>
      <arglist>$var,$val</arglist>
    </func>
    <func name="delSubKey" copy="yes">
      <value>#addkey $dbVar {%expanddb(@{$var}.$key,"|")}
#delkey $dbVar $skey
#addkey %quote($var) $key {$dbVar}</value>
      <arglist>$var,$key,$skey</arglist>
    </func>
    <alias name="createGirl2" copy="yes">
      <value>#call @addSubKey(childrenV,girls,Amy)
#call @addSubKey2(childrenV,girls,Amy,hair,red)
#call @addSubKey2(childrenV,girls,Amy,eyes,brown)
#call @addSubKey2(childrenV,girls,Amy,torso,"Green blouse")
#call @addSubKey2(childrenV,girls,Amy,legs,"brown skirt")
#call @addSubKey2(childrenV,girls,Amy,feet,"green slippers")</value>
    </alias>
    <alias name="createboy1" copy="yes">
      <value>#call @addSubKey(childrenV,boys,Bobby)
#call @addSubKey2(childrenV,boys,Bobby,hair,Black)
#call @addSubKey2(childrenV,boys,Bobby,eyes,Green)
#call @addSubKey2(childrenV,boys,Bobby,torso,"White Jerkin")
#call @addSubKey2(childrenV,boys,Bobby,legs,"Leather Trousers")
#call @addSubKey2(childrenV,boys,Bobby,feet,"Nike Sneakers")</value>
    </alias>
    <alias name="Schildren" copy="yes">
      <value>#loopdb @childrenV {
  $key=%key
  $val=%val
  #loopdb $val {
    $hisher=%if( $key==boys, his, her)
    #show {%key has %val.hair hair and is wearing a %val.torso on $hisher torso and %val.legs covering $hisher legs.}
    }
  }</value>
    </alias>
    <alias name="createboy2" copy="yes">
      <value>#call @addSubKey(childrenV,boys,Jake)
#call @addSubKey2(childrenV,boys,Jake,hair,blonde)
#call @addSubKey2(childrenV,boys,Jake,eyes,blue)
#call @addSubKey2(childrenV,boys,Jake,torso,"a red silk shirt")
#call @addSubKey2(childrenV,boys,Jake,legs,"some purple Trousers")
#call @addSubKey2(childrenV,boys,Jake,feet,"a pair of leather boots")</value>
    </alias>
    <alias name="createChildren" copy="yes">
      <value>createboy1
createboy2
creategirl1
creategirl2</value>
    </alias>
  </class>
</cmud>

To give you an idea of how it works I created some aliases to create a variable named childrenV.
If you type createchildren it will populate the variable with four children.
To see a little bit about the children type schildren.
To look at how you would go about creating the children look at the aliases creatboy1 createboy2 creatgirl1 and creategirl2

An example of how to use the function checkSubKey
#show @CheckSubKey(childrenV,bobby)
Shows you that he is a boy by returning the key boys

An example of how to use the function delSubKey

#call @delSubKey(childrenV,boys,Bobby)
schildren
Note that Bobby's information is gone.
_________________
Arminas, The Invisible horseman
Windows 7 Pro 32 bit
AMD 64 X2 2.51 Dual Core, 2 GB of Ram
Reply with quote
Progonoi
Magician


Joined: 28 Jan 2007
Posts: 430

PostPosted: Wed Oct 22, 2008 6:27 pm   
 
Well, to tell you the truth I only have a faint understanding how these functions work, though I really appreciate the work you've put into it.

I'm going to take another cracks at it, but I won't need very complex system. There will be in most cases not more than 3 values for a key and each of those values always have each a sub-value which is integer.

Basically, a scheme like this:

Code:

key = 
            value1=5
            value2=6
            value3=7


I'm sure eventually I'll understand what's going on here, but I somehow believe that my current way of data extraction with %subregex and
then populating several local vars for cleaner picture within the rest of the code might make more sense to me.

Unless, of course, this way here gives a huge speed gain.

Anyway, thanks again.
_________________
The Proud new owner of CMud.

--------------------------------
Intel Core i5-650 3,2GHz
4 DD3 RAM
GTX 460 768MB
Win 7 Home Premium 64x
--------------------------------
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