Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » zMUD General Discussion
ohm
Beginner


Joined: 17 Sep 2002
Posts: 16

PostPosted: Tue Mar 18, 2008 4:20 am   

zMUD Script Challenge - Counting items
 
Hello there,

I'm struggling to come up with a way to effectively count the number of items in a given line of text. To give you an idea of what I'm trying to achieve I'll endeavour to explain the problem a little more.

I have a variable, a String List, with a few entries in it. Lets call it "stuff". So:

@stuff = ring|watch|glasses|scarf

Now when I look at something MUD wise I have a line of text, something like:

The shelf contains a silver ring, a gold ring, a pocket watch and a green book.

What I really want to do is to be able to count how many items the shelf has on it where the item appears in @stuff.

I've been trying to use %numwords (I.E. "%numwords( "shelf text here", @stuff)") with little result, and fear this is because the function doesn't support String Lists. Are there commands out there that might? Does anyone have any examples I could use to help me on my way?

Many thanks!
Reply with quote
shalimar
GURU


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

PostPosted: Tue Mar 18, 2008 4:33 am   
 
%numitems(@stuff)
_________________
Discord: Shalimarwildcat
Reply with quote
ohm
Beginner


Joined: 17 Sep 2002
Posts: 16

PostPosted: Tue Mar 18, 2008 5:32 am   
 
%numitems seems to only tell me the number of items in @stuff, what I'm hoping to do is find out how many items the shelf has on it that are in @stuff.

What I have come up with, which works but is hardly very elegant is:

#forall @stuff {
count = %numwords( "%1", "%i")
#if (@count > 1) {CountStuffOnShelf = %eval( @count-1)}
}

This bit of zMUD script does the following:
- Loops through all the elements of @stuff (I.E. ring, watch, glasses, etc.).
- Counts how many times each element (%i) occurs in the text (%1 - e.g. defined by trigger pattern) with %numwords.
- If the count is greater than one we've found something! %numwords returns one to many results when using the delimited by
string option for the total number of items. So we subtract 1 and hey presto!

As I said it's not an elegant solution and I'd love to hear back from anyone who has done something similar differently.

All the best!
Reply with quote
shalimar
GURU


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

PostPosted: Tue Mar 18, 2008 6:32 am   
 
http://forums.zuggsoft.com/forums/viewtopic.php?t=13641&


works good
_________________
Discord: Shalimarwildcat
Reply with quote
Dharkael
Enchanter


Joined: 05 Mar 2003
Posts: 593
Location: Canada

PostPosted: Tue Mar 18, 2008 2:25 pm   
 
Currently it seems when your loop is finished only the last word in @stuff gets counted
Shouldn't CountStuffOnShelf be added up.
Code:
#var CountStuffOnShelf {}
#forall @stuff {
#add CountStuffOnShelf %eval(%numwords("%1","%i")-1)
}
_________________
-Dharkael-
"No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style."
Reply with quote
ohm
Beginner


Joined: 17 Sep 2002
Posts: 16

PostPosted: Wed Mar 19, 2008 12:33 am   
 
You're certainly right Dharkael! Just a slight oversight on my part... Embarassed

But with your addition it works as it should, I was just hoping that there might be a nicer way to do it (not that I'm unhappy about how it's working now, just seeing if there was some obscure/hidden function that could handle comparing String Lists with text).

Thanks for your example shalimar, but again I find it more usuful for counting things indescriminately, rather than a list of items you have prepared. As it's only certain things I want to count I have to be able to determine what's worth counting in the script.
Reply with quote
Dharkael
Enchanter


Joined: 05 Mar 2003
Posts: 593
Location: Canada

PostPosted: Wed Mar 19, 2008 1:52 am   
 
definitely not sexy but if you're looking for an alternative imp...
Code:
#echo %len(%subregex(%mss(%concat(%string("%1"),".replace(/",(@stuff),"/g,'\x00')"),"JScript"),"[^\x00]"))
_________________
-Dharkael-
"No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style."
Reply with quote
rmorock8869@yahoo.com
Beginner


Joined: 15 May 2018
Posts: 17

PostPosted: Tue May 15, 2018 3:08 pm   Counting an item in my cloak
 
I am a super newb with cmud. Currently I am playing a character in forgotten kingdoms. I would like to have a count of daggers in my cloak displayed on the status bar at the bottom since I am trying to level up my throwing dagger skills. Can anyone help me with a script please
Reply with quote
shalimar
GURU


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

PostPosted: Tue May 15, 2018 5:48 pm   
 
Best to start your own thread rather than hijack someone else's thread, but you want the #STATUS command.

#STATUS {Daggers: @daggerCount}
_________________
Discord: Shalimarwildcat
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » zMUD 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