|
Capriole Newbie
Joined: 12 Dec 2006 Posts: 6
|
Posted: Sun Apr 01, 2012 9:11 pm
alias/variable help - possibly absurdly simple |
I find myself trying to convert my Zmud-oriented scripting knowledge to the wonders of Cmud--and failing badly. Not sure if it's been too long and I lost the way of thinking, or if things are just different enough that I'm lost, or both. After extensive forum and help-file searching, and exploring over-complicated routes of databases and external files, I'm just about ready to cry and submit to a failure-induced migraine...help me wonderful zugg forum kenobes! You're my only hope!
My goal is to be able to save and retrieve mental notes using Cmud instead of going into a word processor, entering information, and later looking it up again, using aliases "mem" and "remember" I.E.:
>mem tavia Likes to disguise as petite half-elf, helpful.
>remember tavia
Likes to disguise as petite half-elf, helpful.
Ideally I'd like to be able to append information to the variable, but I can always just add a number and keep making new variables. I tried to accomplish this with a first alias to create a variable named %1 with a value of the string following the name, which I think is done with %-2 --and then later use the #SHOW command to display the string entered as the variable's value.
I'm probably way off but my attempts are doing nothing but helpfully displaying the variable name upon request; with:
Code: |
#CLASS {MENTALNOTES}
#ALIAS mem {#VA %1 %-2}
#ALIAS remember {#SHOW @%1}
#CLASS 0 |
QUESTIONS:
Am I approaching this the right way?
If not, can anyone point me to the function I should be fumbling with?
Should I be trying to use a database or the file functions instead?
Is this as mind-bogglingly simple as it seems like it should be and I'm missing something basic, or is this actually complicated and I'm being unrealistic?
Thanks in advance for any help or advice! |
|
|
|
Rahab Wizard
Joined: 22 Mar 2007 Posts: 2320
|
Posted: Mon Apr 02, 2012 12:52 am |
You are correct that %-2 will give you all the parameters after the first parameter. Another way to do it is %params(2), which is the way I usually do it.
To do it all in one variable, you want to use database variables. Look up #ADDKEY, #DELKEY, %db, and related terms for some more information. In essence, a database variable is a list of "key=value" pairs. You can pull out a particular value by specifying the associated key. Once you understand database variables, what you want is in fact quite easy.
Here is a simple way to do what you want with database variables:
Code: |
#CLASS MENTALNOTES
#VAR memories {}
#ALIAS mem {#ADDKEY memories %1 %params(2)}
#ALIAS remember {#SHOW %db(@memories, %1)}
#CLASS 0
|
One problem with this simple code is that you can only store one "fact" in a given key. If you use mem with the same key again, it will replace the current fact with the new fact. A nice thing about the current Cmud version is that db variable values can themselves be complex things like stringlists or even lists of key=value pairs. So, it is possible to gather a series of facts about a key as a stringlist. The following code will add a new fact each time you use mem with a given key. The remember alias will list all the facts for a key on separate lines. In addition I will add an alias to "forget" all facts about a given key:
Code: |
#CLASS MENTALNOTES
#VAR memories {}
#ALIAS mem {
$facts = %db(@memories, %1)
$newfact = %params(2)
#IF ($facts) {#ADDITEM $facts $newfact} {$facts = $newfact}
#ADDKEY memories %1 $facts
}
#ALIAS remember {#FORALL %db(@memories, %1) {#SHOW %i}}
#ALIAS forget {#DELKEY @memories %1}
#CLASS 0
|
|
|
|
|
Capriole Newbie
Joined: 12 Dec 2006 Posts: 6
|
Posted: Mon Apr 02, 2012 5:49 am |
Perfect! Thank you so much! I think I can work backwards from your example to let me play with databases further, which makes your help exponentially even more useful. :) I, and the characters previously limited by my short memory span, thank you!
|
|
|
|
|
|
|
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
|
|