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
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4690
Location: Pensacola, FL, USA

PostPosted: Wed Dec 28, 2011 2:40 pm   

#ADDKEY question
 
What is the proper syntax for indirectly assigning a value to a db variable?


Code:
$curValue=@spikeRates.$item.$spike.Failure
$newVal=($curValue+1)
#ADDKEY spikeRates.$item.$spike {Failure=$newVal}
#FORALL %dbkeys( @spikeRates.$item) {#SHOWDB spikeRates.$item.%i}


I have tried using a normal variable for $item and $spike as well, no no avail.
I'm probably so tired I'm just overlooking the obvious.
_________________
Discord: Shalimarwildcat
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Wed Dec 28, 2011 6:06 pm   
 
Huh?

#ADDKEY varName Key Value

#ADDKEY myDB %concat($item.$spike, ".Failure") $newVal

Maybe that? I'm sort of confused of the specifics of what you're trying to do here. I tend to avoid using a DB var directly as the key when possible, preferring to grab that specific value in a local variable and using that as the key. If only because it's a bit easier on the eyes.
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4690
Location: Pensacola, FL, USA

PostPosted: Wed Dec 28, 2011 10:22 pm   
 
I am trying to record the success rates of a given skill, by both the item worked and quality of materials used.

And i am trying to reference by local variables, that is exactly what I am asking for help with.
Your example doesn't seem to work any better than mine did.
_________________
Discord: Shalimarwildcat
Reply with quote
charneus
Wizard


Joined: 19 Jun 2005
Posts: 1876
Location: California

PostPosted: Wed Dec 28, 2011 10:45 pm   
 
Have to use %db(%db(spikeRates, $item), $spike) to reference them as such. At least, that's how I've done it in the past.
Reply with quote
Anaristos
Sorcerer


Joined: 17 Jul 2007
Posts: 821
Location: California

PostPosted: Thu Dec 29, 2011 1:05 am   
 
Tests show that your scheme should work. The only thing that could be making it fail is the contents of $item and/or $spike.

I am assuming, of course, that what you have a is a db record cascade. (a db record for which each value assigned to a key is also a db record, recursively).
_________________
Sic itur ad astra.
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4690
Location: Pensacola, FL, USA

PostPosted: Thu Dec 29, 2011 12:34 pm   
 
Yes, i have the name of the db, more or less just a string list of each items ever worked.
Under that are the various quality materials.
Each of which has a success/failure count.

No key at any level is presumed to be preexisting.

Referencing the item is not the issue (i dont see how %db does more than this), that aspect seems to work when i had used #SAY to error test my variables
But it doesn't seem to like to make new keys on the fly at that deep a level.
_________________
Discord: Shalimarwildcat
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4690
Location: Pensacola, FL, USA

PostPosted: Thu Dec 29, 2011 1:24 pm   
 
In an effort to try and force the depth of creation i have done the following... with some odd results.

Code:
spikeRates=""
#IF (%null( @spikeRates.$item)) {
  #ADDITEM spikeRates $item
  #FORALL $quality {
    $this=%i
    #ADDITEM spikeRates.$item $this
    #FORALL "Success|Failure" {
      $that=%j
      #ADDKEY spikeRates.$item.$this {$that=0}
      }
    }
  }
$curValue=@{spikeRates.$item.$spike.Failure}
$newVal=($curValue+1)
#SHOWDB @spikeRates
#EXIT

Which then showed me this:

spaces added for screen width wrote:
1: character
character: iron|steel|silver
12289: |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"Success=0|Failure=0|Success=0|Failure=0|Success=0|Failure=0"
_________________
Discord: Shalimarwildcat
Reply with quote
MattLofton
GURU


Joined: 23 Dec 2000
Posts: 4834
Location: USA

PostPosted: Thu Dec 29, 2011 9:04 pm   
 
There are some situations where @var.field using indirection reverts to concatenation. You will ABSOLUTELY, with no workaround, have to use %db() instead of dot-notation. It's a side-effect brought in when JSON functionality was enabled, allowing us to go more than one level deep.
_________________
EDIT: I didn't like my old signature
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4690
Location: Pensacola, FL, USA

PostPosted: Thu Dec 29, 2011 9:48 pm   
 
Then show me how to use %db to set a value, the help file says it only retrieves.
_________________
Discord: Shalimarwildcat
Reply with quote
Daern
Sorcerer


Joined: 15 Apr 2011
Posts: 809

PostPosted: Thu Dec 29, 2011 9:51 pm   
 
You don't. Use %db to retrieve the value to a local variable, then make the required changes to that local variable, then use #ADDKEY to replace the existing data in your variable with the local one you just made the changes to.
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4690
Location: Pensacola, FL, USA

PostPosted: Thu Dec 29, 2011 10:03 pm   
 
I could really use an example here, in case you can't tell.
_________________
Discord: Shalimarwildcat
Reply with quote
charneus
Wizard


Joined: 19 Jun 2005
Posts: 1876
Location: California

PostPosted: Fri Dec 30, 2011 12:44 am   
 
Code:
#ADDKEY %db(spikeRates, $item) $spike {Failure=$newval}


That should do what you need it to do.

Of course, if you're willing, you can convert it all to Lua, which would make it a lot easier.
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4690
Location: Pensacola, FL, USA

PostPosted: Fri Dec 30, 2011 7:10 am   
 
Well seems there is still an issue..

Code:
#ADDKEY spikeRates {$item="iron|steel|silver"}
#ADDKEY %db( spikeRates, $item) $spike {Failure=0}
#ADDKEY %db( spikeRates, $item) $spike {Success=0}


With this i am able to get it made at the level I want... however it only seems to retain the Success value, Failure is lost somewhere

Well I'm not against learning a new language, i have heard so much about lua.
_________________
Discord: Shalimarwildcat
Reply with quote
charneus
Wizard


Joined: 19 Jun 2005
Posts: 1876
Location: California

PostPosted: Fri Dec 30, 2011 9:56 am   
 
Not sure why it's not working for you on that. Hmm.

As for Lua, I've converted many of my scripts (including databases) over to using Lua variables. It would ultimately depend on what you're doing with your script and how you're getting information, but for the #ADDKEY stuff above, it'd look something like:
Code:
for _,v in pairs(spikeRates) do
v={Failure=0, Success=0}
end


Then for the newval part, you could do:
Code:

newval = spikeRates["steel"]["Failure"]+1


Lastly, if you wanted to print the spikeRates database, you could do:
Code:
for i,v in pairs(spikeRates) do print(i .. ": " .. v) end


There's a lot to learn about Lua, but it's relatively simple. If you want to get started, you can read through Lua Users Wiki.

Hope this helps you out some. If you want some more help on converting anything over, just let me know and I'll do what I can. By the way, if you want to save those variables for a long run, google table.save lua, save the file in CMUD's Lua directory as tsave.lua, then create an onLoad event in CMUD that does:
Code:
#LUA {require("tsave")}


Then to save a table, you just have to execute the command (in Lua): table.save(tablename, "filename you want to save it as") (example: table.save(spikeRates,"spikeRates.tbl")

To recall that table, in the onLoad event, add
Code:
#LUA {spikeRates=table.load("spikeRates.tbl")}


It's very useful, and I'm glad Lua is included with CMUD. Just keep in mind that Lua IS case-sensitive. spikeRates is completely different from Spikerates and other variants.
Reply with quote
MattLofton
GURU


Joined: 23 Dec 2000
Posts: 4834
Location: USA

PostPosted: Fri Dec 30, 2011 5:49 pm   
 
You have to do it all at once. #ADDKEY is a replacement function, rather than an appending one.

#ADDKEY variable key value
_________________
EDIT: I didn't like my old signature
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4690
Location: Pensacola, FL, USA

PostPosted: Fri Dec 30, 2011 6:05 pm   
 
MattLofton wrote:
You have to do it all at once. #ADDKEY is a replacement function, rather than an appending one.

#ADDKEY variable key value


If you can show me an example using my local variables that actually makes the table down to the 4th level, like I have tried to explain/outline in this thread... great, THAT is what I am asking for help with.
Quoting the help file syntax doesn't help. I have read it countless times over.
_________________
Discord: Shalimarwildcat
Reply with quote
Rahab
Wizard


Joined: 22 Mar 2007
Posts: 2320

PostPosted: Tue Jan 03, 2012 1:55 pm   
 
Looking at your last example:
Code:

#ADDKEY spikeRates {$item="iron|steel|silver"}
#ADDKEY %db( spikeRates, $item) $spike {Failure=0}
#ADDKEY %db( spikeRates, $item) $spike {Success=0}

The problem here is that #ADDKEY will completely replace the value of the key. So the value of the $spike key is being replace with {Failure=0}, and then replaced by {Success=0}.

Instead, you can do this:
Code:

#ADDKEY spikeRates {$item="iron|steel|silver"}
#ADDKEY %db( spikeRates, $item) $spike {Failure=0|Success=0}


If you need to change the Failure or Success value separately, you can do something like:
Code:

$spikevalue = @spikeRates.$item.$spike
#ADDKEY $spikevalue Failure 1
#ADDKEY %db(spikeRates, $item) $spike $spikevalue

I haven't tried this, but you might be able to do something like:
Code:

$spikevalue = @spikeRates.$item.$spike
#ADDKEY %db(%db(spikeRates, $item), $spike) Failure 1
Reply with quote
DraxDrax
Apprentice


Joined: 22 Mar 2009
Posts: 149

PostPosted: Thu Jan 05, 2012 8:02 pm   
 
Code:
$item = "widget"
$spike = "steel"
#ADDKEY spikeRates.${item} {iron|steel|silver} ""
#ADDKEY spikeRates.${item}.${spike} Failure 0
#ADDKEY spikeRates.${item}.${spike} Success 0


Try that. The code works. I'm just not certain I've understood your intentions from the examples given above. To better illustrate what it's doing, the following four lines would set up the exact same table of values for spikeRates, using constants rather than variables in the declarations:

spikeRates.widget.steel.Failure = 0
spikeRates.widget.steel.Success = 0
spikeRates.widget.iron = ""
spikeRates.widget.silver = ""

edit: I reviewed your first post, rather than your last post, which I probably should have done to begin with...
I believe this is more or less what you were after in the first place. Running this code will increment the value of spikeRates.widget.steel.Failure using temporary variables, by 1 each time it's executed and shows the update.
Code:
$item = "widget"
$spike = "steel"
$value = %db(%db(%db(@spikerates, $item), $spike), Failure)
$value = $value+1
#ADDKEY spikeRates.${item}.${spike} Failure $value
#SHOWDB @spikeRates.widget
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4690
Location: Pensacola, FL, USA

PostPosted: Wed Jan 11, 2012 2:43 pm   
 
That last bit is working for me.
Thank you kindly.

Dunno why i didn't think to use ${var} i use @{var} a lot.
_________________
Discord: Shalimarwildcat
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