 |
cachew Novice
Joined: 10 May 2001 Posts: 39 Location: USA
|
Posted: Sat Aug 25, 2007 9:09 am
Couple questions: Counting Lines and Alphabetize list |
I have 2 questions.
Quetions #1 I am trying to write a trigger that will show me the # of items in my locker. The syntax is just eluding me.
The text will be as follows:
Code: |
You look in your locker and see:
a clear jar
a dagger of accuracy
X a gleaming, platinum mace
-->a shield with the seal of New Ashton
a writhing tendril of living mist
a wooden cane
-->delicate silk filaments of a spider's web
a horned war helmet
X Takir's wristband
a brass sextant
X a golden studded mace
! the gauntlets of dexterity
!-->an ancient crimson shield with the symbol of a white chalice
!-->black iron gauntlets
!-->an ancient crimson shield with the symbol of a white chalice
!-->black iron gauntlets |
I would like it to count the number of lines after the, "you look in the locker and see:" and then display a line that say something like "You have X items in your locker."
I know this has to be looped and use I assume use %count or %countlist I just cant figure out how that is really done. I have tried several iterations to no avail. Although it doesn't show up there are 5 spaces before each item name above, Im not sure if that makes any difference. I assume you would trigger on the first line then check to see if another non-blank line exists and if so add 1 to the counter variable. Once you hit a blank line the print the summary line. I have the conecpt just can't get it into code.
Second question is: How would you read in a list on items, alphabetize them and redisplay them to the screen. The following is an snippet of the list:
Code: |
You can practice any of these spells:
[ Teleport][ (Superb)][ 35]
[ Blindness][ (Superb)][ 5]
[ Burning Hands][ (Superb)][ 10]
[ Charm Person][ (Superb)][ 5]
[ Chill Touch][ (Superb)][ 10]
[ Colour Spray][ (Superb)][ 15]
[ Curse][ (Superb)][ 20]
[ Detect Invisibility][ (Superb)][ 5]
[ Detect Magic][ (Superb)][ 5]
[ Enchant Weapon][ (Superb)][ 100]
[ Energy Drain][ (Superb)][ 22]
[ Fireball][ (Superb)][ 18]
[ Invisibility][ (Superb)][ 5]
[ Lightning Bolt][ (Superb)][ 15]
[ Locate Object][ (Superb)][ 20]
[ Magic Missile][ (Superb)][ 8]
[ Shocking Grasp][ (Superb)][ 12]
[ Sleep][ (Superb)][ 15]
[ Strength][ (Superb)][ 20]
*** Press return or Q to abort ***
[ Breathe Water][ (Superb)][ 10]
[ Shield Room][ (Superb)][ 75]
[ Chain Lightning][ (Superb)][ 28]
[ Map Catacombs][ (Superb)][ 50] |
I would like to aplhabetize these based upon the spell name. The ***Press ... portion above happens when the lst becomes a bit long but when enter is pressed the ***Press ... message disappears so I am not sure if it must be accounted for. There will be some empty spaces in the middle of the list as it breaks across "pages" of spells. I assume I would need to read everything in first, then sort them, then redisplay them. One the first question I have been playing around with a few ideas but on this I am at a loss as to how to proceed. Anyone have any ideas? |
|
_________________ Christopher Chew |
|
|
 |
Arminas Wizard
Joined: 11 Jul 2002 Posts: 1265 Location: USA
|
Posted: Sat Aug 25, 2007 12:56 pm |
First we need to know what your prompt looks like. And Or any text you see at the very end of these lists.
Here is what I have made so far.
First problem.
#trigger {^You look in your locker and see:} {#var LockerI "";#T+ LockerA}
#trigger LockerA {(*)} {LockerI=%additem( "%1", @lockerI)}
#alias CountLocker {#T- LockerA;#say %eval( %numitems( @lockerI)-1)}
For this to work properly you need to create a trigger that fires on the text that you get at the end of the list as I mentioned. Then place the countLocker alias within it. You may have to change the -1 to -2 if this captures your prompt. |
|
_________________ Arminas, The Invisible horseman
Windows 7 Pro 32 bit
AMD 64 X2 2.51 Dual Core, 2 GB of Ram |
|
|
 |
Fang Xianfu GURU

Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Sat Aug 25, 2007 12:59 pm |
EDIT: Hardly surprising that Arminas beat me :( Oh well.
Hope you don't mind, I added some code tags to your post so that the forums didn't eat the spaces.
Okay... the first problem you're going to have to address is that the script will need to know when both lists have ended. Knowing when they start is easy, because they have some unique text at the beginning. Without some text at the end to indicate that the list has ended, the script has no way of knowing when to move on from the "capturing items in the list" stage to doing whatever you need it to do to modify the list.
Firstly, an actual trigger to match items in the list. For the first list, you have a couple of options - you could either match on EVERYTHING, until you receive whatever line ends the list, or you could write a trigger that's as specific as possible. I'm very much an advocate of the latter, so here goes.
This pattern:
^{X| }{!| }{-->| }[A-Za-z ] |
should match all the lines you have there, but I haven't tested it. Hopefully you get the idea.
Now, here's a simple script that uses it:
#trig {You look in your locker and see:} {#var NumItems 0;#t+ LockerTrig}
#cond {some line that ends the list} {#t- LockerTrig;#say You have @NumItems items in your locker.}
#trig "LockerTrig" {^{X| }{!| }{-->| }[A-Za-z ]} {#add NumItems 1} "" {disable}
EDIT: Argh, the forums are eating my spaces - make sure you paste the pattern in the code tag above, and not the version in that script.
If the line that ends the list is your prompt, it'll look a bit pants having the number of items after it, so you could try this instead of #say You have @NumItems items in your locker.
#gag;#say You have @NumItems items in your locker.;#say %line
You can apply similar logic to the second list:
#trig {You can practice any of these spells:} {#var Spells "";#t+ SpellTrig;#gagon}
#cond {some line that ends the list} {#gagoff;#t- SpellTrig;#forall %sort(@spells) {#say %i}}
#trig "SpellTrig" {^~[%s[A-Za-z ]~]~[%s~(%w~)~]~[%s%d~]} {#additem Spells {%line}} "" {disable}
This is slightly more complex - it begins gagging every line that's sent from the MUD so they don't appear on the screen, and then stops the gagging when the list has ended. While that was happening, every time a spell line was received, that line's added to a list. Once the list finishes, %sort is used to sort the list, and then it goes through every item, printing them out again.
Adding support for multiple pages to either of these scripts is simple:
#trig "PageSkip" {~*~*~* Press return or Q to abort ~*~*~*} {#send %cr}
Then just add the commands #t+ PageSkip and #t- PageSkip when you want to start and stop page skipping. |
|
|
 |
Arminas Wizard
Joined: 11 Jul 2002 Posts: 1265 Location: USA
|
Posted: Sat Aug 25, 2007 1:40 pm |
Heh, I stopped working on the second solution to help progonoi with something. Fang did basically what I was going to suggest so I will just leave it at that.
|
|
_________________ Arminas, The Invisible horseman
Windows 7 Pro 32 bit
AMD 64 X2 2.51 Dual Core, 2 GB of Ram |
|
|
 |
cachew Novice
Joined: 10 May 2001 Posts: 39 Location: USA
|
Posted: Sat Aug 25, 2007 8:50 pm |
Sorry for not mentioning the end of the list it would be an empty space and then my prompt (I was trying to be thorough) which would be <45hp 50mp 200mv 100br>. That is great. I am continually surprised at how easy you all make this look. This is such a major effrot for me to try and figure out reading tutorials, writing notes, trying 10 things that dont work. Thanks again for the help.
|
|
_________________ Christopher Chew |
|
|
 |
|
|