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
Zenkai
Beginner


Joined: 11 Nov 2000
Posts: 10
Location: USA

PostPosted: Sat Jan 05, 2002 2:32 pm   

List Function Problem
 
zMUD has this function

syntax: %numitemss(list)
description: return the number of items in a stringlist.

example: #SHOW %numitems(Athos|Bertrand|Caesar|Bertrand|Caesar|Bertrand)
displays: 6

---

I want a function like this

syntax: %numitemss(list,s)
description: return the number of items named s in a stringlist.

example: #SHOW %numitems(Athos|Bertrand|Caesar|Bertrand|Caesar|Bertrand,Athos)
displays: 1

example: #SHOW %numitems(Athos|Bertrand|Caesar|Bertrand|Caesar|Bertrand,Bertrand)
displays: 3

example: #SHOW %numitems(Athos|Bertrand|Caesar|Bertrand|Caesar|Bertrand,Caesar)
displays: 2

---

Any ideas how to code this list function?
Reply with quote
LightBulb
MASTER


Joined: 28 Nov 2000
Posts: 4817
Location: USA

PostPosted: Sat Jan 05, 2002 4:10 pm   
 
Set counter to zero. Loop through list checking each item to see if it matches. Increment counter whenever it does. Display counter.

LightBulb
All scripts untested unless otherwise noted
Reply with quote
Zenkai
Beginner


Joined: 11 Nov 2000
Posts: 10
Location: USA

PostPosted: Sat Jan 05, 2002 6:12 pm   
 
This works for the above example, guess there isn't a pretty
solution for the general case

#var item1 0
#var item2 0
#var item3 0
#var list {Athos|Bertrand|Caesar|Bertrand|Caesar|Bertrand}
#var numlist %numitems(@list)
#var dlist %dups(@list)
#loop 1,@numlist {#if (%item(@dlist,1) = %item(@list,%{i})) {#add item1 1}}
#loop 1,@numlist {#if (%item(@dlist,2) = %item(@list,%{i})) {#add item2 1}}
#loop 1,@numlist {#if (%item(@dlist,3) = %item(@list,%{i})) {#add item3 1}}
#echo %item(@dlist,1) occurs @item1 times
#echo %item(@dlist,2) occurs @item2 times
#echo %item(@dlist,3) occurs @item3 times

Would be nice if you could code nest loops with counters.

I'm surprized zmud doesn't have basic arrays, something like

example: #show %replaceitem("Athos|Bertrand|Caesar","Zugg",2)
display: Athos|Zugg|Caesar

Zenkai -- SlothMUD III
Reply with quote
MattLofton
GURU


Joined: 23 Dec 2000
Posts: 4834
Location: USA

PostPosted: Sun Jan 06, 2002 3:07 am   
 
It's possible to use just two loops (in nested formation) and still be able to match any number of entries at one time.

li'l shmoe of Dragon's Gate MUD
Reply with quote
Kjata
GURU


Joined: 10 Oct 2000
Posts: 4379
Location: USA

PostPosted: Sun Jan 06, 2002 4:32 pm   
 
quote:

I'm surprized zmud doesn't have basic arrays, something like

example: #show %replaceitem("Athos|Bertrand|Caesar","Zugg",2)
display: Athos|Zugg|Caesar



You can do exatly that but you have the syntax wrong. It is:
#SHOW %replaceitem("Zugg", 2, "Athos|Bertrand|Caesar")

Kjata
Reply with quote
Troubadour
GURU


Joined: 14 Oct 2000
Posts: 556
Location: USA

PostPosted: Sun Jan 06, 2002 5:41 pm   
 
quote:

Would be nice if you could code nest loops with counters.



You can! Just save the iterands of the outer loops to a temporary variable.


#VAR list {Athos|Bertrand|Caesar|Bertrand|Caesar|Bertrand}
#VAR dlist %dups(@list)
#FORALL @dlist {
#VAR tempitem %i
#VAR total 0
#FORALL @list {
#IF (%i = @tempitem) {#ADD total 1}
}
#SHOW @tempitem occurs @total times~.
}



Troubadour
Reply with quote
Zenkai
Beginner


Joined: 11 Nov 2000
Posts: 10
Location: USA

PostPosted: Mon Jan 07, 2002 12:49 am   
 
Sweet! I can code a nested loop.
I should have thought of using my own counter. *kick self*

Here is the code I got to work:
 

#var list {Athos|Bertrand|Caesar|Bertrand|Caesar|Bertrand}
#var dlist %dups(@list)
#var numlist %numitems(@list)
#var numdlist %numitems(@dlist)
#echo List: @list
#var dlistcounter 0
#loop 1,@numdlist {#var item 0
#add dlistcounter 1
#loop 1,@numlist {#if (%item(@dlist,@dlistcounter) = %item(@list,%{i})) {#add item 1}}
#echo %item(@dlist,@dlistcounter) occurs @item~x}

Output:

List: Athos|Bertrand|Caesar|Bertrand|Caesar|Bertrand
Athos occurs 1x
Bertrand occurs 3x
Caesar occurs 2x

Thanks Mattloftan and Troubadour! :)

To: Kajata

The replaceitem command is in new version? Very Cool. I'm using version 5.55 here because I mud from a lot different computers.
 
Reply with quote
Kjata
GURU


Joined: 10 Oct 2000
Posts: 4379
Location: USA

PostPosted: Mon Jan 07, 2002 2:32 pm   
 
Well, you could code your own version of %replaceitem in 5.55 by creating a copy of the list, looping thorught it and writing it item by item to the original list while replacing the specified one with what is needed.

Also, you should try looking at the %countlist function (by looking at the online helpfile it appears to be available in 5.55) which does exactly what you coded.

Kjata
Reply with quote
Zenkai
Beginner


Joined: 11 Nov 2000
Posts: 10
Location: USA

PostPosted: Wed Jan 09, 2002 8:50 am   
 
Thanks Kjata, I'll use the countlist function - it will simply my code a lot.

*sigh* That function is listed under database functions, and I don't use the database module so I skipped over it...
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