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

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » Finished MUD Scripts
big_luke
Newbie


Joined: 11 Jun 2005
Posts: 9

PostPosted: Tue Jun 14, 2005 2:12 am   

List Function: Longest Member
 
I've written a little function that will tell you what the length of the longest member of a list is. The script itself is as follows:

Code:
#function longestmemberinternal {%if( %1<%numitems( @%2), @longestmemberinternal(%eval(%1+1),%2,%max(%3,%len(%item(@%2,%1)))), %max( %3, %len( %item( @%2, %1))))}
#function longestmember {%if( %numitems( @%1)>1, @longestmemberinternal(1,%1,0), %len( %item( @%1,1)))}


Entering that in your command prompt should properly register the functions, but you might want to check just to make sure.

The usage is as follows:

@longestmember(varname)

Where varname is the name of the list. For example, if you have a variable "Targetlist" which you access with @targetlist, you will want to do @longestmember(targetlist).

The function returns an integer value which is as if you had done %len on the longest member of the list.

I have not extensively tested this function, though it does work and I would anticipate that it will work in all cases where it was used properly.

Enjoy.


Last edited by big_luke on Sun Jul 03, 2005 10:54 am; edited 1 time in total
Reply with quote
Full Throttle
Wanderer


Joined: 07 Dec 2004
Posts: 65

PostPosted: Fri Jul 01, 2005 3:21 am   
 
Sweet function Smile

I think you forgot the ",1" in the %item function within longestmember.

Is it possible to write a similiar function for the length of the longest key in a database variable?

#addkey db {x=7|xx=5|xxx=1}
#echo @longestkey(db)
3

#addkey db {x=7|xx=5}
#echo @longestkey(db)
2

#addkey db {x=7}
#echo @longestkey(db)
1


Last edited by Full Throttle on Fri Jul 01, 2005 3:48 am; edited 3 times in total
Reply with quote
Orang
Apprentice


Joined: 22 Jul 2004
Posts: 118
Location: USA

PostPosted: Fri Jul 01, 2005 3:24 am   
 
i dont have time to test this right now but isnt this the same thing as #FORALL?

#FORALL @targetlist {#ECHO %i}

shows everything in the @targetlist var

but maybe i'm missing something because i cant test right now
Reply with quote
big_luke
Newbie


Joined: 11 Jun 2005
Posts: 9

PostPosted: Sun Jul 03, 2005 10:44 am   
 
Full Throttle wrote:
I think you forgot the ",1" in the %item function within longestmember.


You're right, it is missing a ,1 from it, but for some strange reason, seems to work without it. Just to be sure, I've edited my original post to include it.

Did you notice any specific problems with it the way it was? I'd love to know what kind of whacky behaviour it was producing (if any), because some of the best coding discoveries come out of whacky mistakes.

Full Throttle wrote:

Is it possible to write a similiar function for the length of the longest key in a database variable?


You can in fact use the existing function for this purpose, with not too much trouble.

Code:

example=%dblist(db)
#ech @longestmember(example)


The output in your previous example should match the values you suggested. %dblist takes one mandatory parameter of the name of the database column (in this case db), and returns all the values in that column as a list (which is what @longestmember likes Wink )

I could modify longestmember to take a list, instead of the name of a variable which is a list, in which case you could use the afforementioned code directly with:

Code:

#ech @longestmember(%dblist(db))


I'll work on it and test it out, and post the alternate version here for you if you wish.

Drang wrote:

i dont have time to test this right now but isnt this the same thing as #FORALL?

#FORALL @targetlist {#ECHO %i}

shows everything in the @targetlist var

but maybe i'm missing something because i cant test right now


No, the purpose of this function is to return an integer value which gives the length of the longest member of a list, in characters. Your example would merely display each member of the list, which is not what this function is used for.

Thanks for the feedback.

Edited to Add: The problem was in fact unnoticed because it was only called if you passed @longestmember a list that had only one member, the new version fixes this problem, and will return the correct length instead of nothing if you pass it a single-member list.
Reply with quote
big_luke
Newbie


Joined: 11 Jun 2005
Posts: 9

PostPosted: Sun Jul 03, 2005 11:08 am   
 
Here is the alternate version of longestmember which takes a parameter of a string rather than a reference to a string variables:

Code:

#function longestmember {%if( %numitems( %1)>1, @longestmemberinternal(1,%1,0), %len( %item( %1,1)))}

#function longestmemberinternal {%if( %1<%numitems( %2), @longestmemberinternal(%eval(%1+1),%2,%max(%3,%len(%item(%2,%1)))), %max( %3, %len( %item( %2, %1))))}


Unlike the previous function, this function MAY have troubles with a string list that contains Commas (",") (not to be confused with a string list delimited by commas), which would be exceptionally rare, but I'll work on getting it to work properly like that anyway. Enjoy.
Reply with quote
Full Throttle
Wanderer


Joined: 07 Dec 2004
Posts: 65

PostPosted: Sun Jul 03, 2005 5:42 pm   
 
I thought the %dblist function was just for database records not database variables.

#addkey db {x=7|xx=5|xxx=1}
#var list {x|xx|xxx}
#echo 1: %dblist(db)
#echo 2: @list
1:
2: x|xx|xxx

I could make a list outside the function

#addkey db {x=7|xx=5|xxx=1}
#var list %null
#loopdb @db {#additem list %key}
#echo @longestmember(list)
3

but that is cheating Twisted Evil

PS:

I would like to find the max key length so that I can use it inside the %format function to show multiple database variables in multiple columns - right now I am using a constant width, which displays incorrectly when the key length is too long.

Thanks, F.T.
Reply with quote
Orang
Apprentice


Joined: 22 Jul 2004
Posts: 118
Location: USA

PostPosted: Thu Jul 07, 2005 6:02 pm   
 
is there one i could use to show the *highest* member?
like if it has {1|2|54|52|4} as the definition, it would show 54
Reply with quote
alaric
Novice


Joined: 13 Aug 2005
Posts: 32
Location: Cherry Point, NC

PostPosted: Sun Aug 14, 2005 11:33 am   
 
Just curious, what exactly is this script for? What is it used to do? How could it be used in the MUD?
Reply with quote
big_luke
Newbie


Joined: 11 Jun 2005
Posts: 9

PostPosted: Wed Sep 07, 2005 11:48 am   
 
alaric wrote:
Just curious, what exactly is this script for? What is it used to do? How could it be used in the MUD?


Well, I originally created it because I was playing around with another script to show me (via echos) two lists that were 'linked' in a sense. I wanted the output to display nicely depending on how long the longest word in the list was (i.e. I wanted it to expand or contract automatically). So, I used longestmember on each of the lists to find out the length of the longest word, and made it output that many characters as a border - here's an example (which quite possibly will not come out QUITE right because it's not in fixedsys font):

Code:
+-------+-------+
|.Wrong | Right.|
+-------+-------+
|..Zuug | Zugg..|
+-------+-------+


Later, if I add a few more words to each list...

Code:
+------------+------------+
|......Wrong | Right......|
+------------+------------+
|.......Zuug | Zugg.......|
|.capitalizm | capitalism.|
|........joo | joe........|
+------------+------------+


There is actually a script from another thread in this forum by me which shows the way I implemented this function - most things that you would use this script for would revolve around displaying some kind of formatted output, or possibly, some kind of string manipulation.

I can't think of any uses for actually SENDING things to the mud off the top of my head, but I'm sure there are some. Hope this helps.
Reply with quote
big_luke
Newbie


Joined: 11 Jun 2005
Posts: 9

PostPosted: Wed Sep 07, 2005 11:56 am   
 
Orang wrote:
is there one i could use to show the *highest* member?
like if it has {1|2|54|52|4} as the definition, it would show 54


Done and done with just a slight modification - this script would actually make a fairly good general case for list comparison when I look at it.

Here's the function:

Code:
#function highestmemberinternal {%if( %1<%numitems( @%2), @highestmemberinternal(%eval(%1+1),%2,%max(%3,%item(@%2,%1))), %max( %3, %item( @%2, %1)))}

#function highestmember {%if( %numitems( @%1)>1, @highestmemberinternal(1,%1,0), %item( @%1,1))}


The usage is similar to the last one: @highestmember(varname), for example, if you had a list '@list', you would use @highestmember(list).

It worked on the example numbers you gave, and a few others I tried. If you run into any bugs let me know :)


Last edited by big_luke on Wed Sep 07, 2005 12:20 pm; edited 1 time in total
Reply with quote
big_luke
Newbie


Joined: 11 Jun 2005
Posts: 9

PostPosted: Wed Sep 07, 2005 12:02 pm   
 
Before someone asks, here's how to do the same as above except finding the LOWEST number in a list:

Code:
#function lowestmemberinternal {%if( %1<%numitems( @%2), @lowestmemberinternal(%eval(%1+1),%2,%min(%3,%item(@%2,%1))), %min( %3, %item( @%2, %1)))}

#function lowestmember {%if( %numitems( @%1)>1, @lowestmemberinternal(1,%1,0), %item( @%1,1))}


I'm sure you can figure out usage.

P.S Both highestmember and lowestmember have a curious effect when you give them strings instead of numbers - they show you which one comes first/last alphabetically. I'm sure you can find good use for this :)
Reply with quote
Vitae
Enchanter


Joined: 17 Jun 2005
Posts: 673
Location: New York

PostPosted: Mon Nov 07, 2005 3:43 pm   
 
Not sure why, but the lowestmember does not seem to work

#VAR numericaltest {2|9|5|15|4|10}
#function lowestmemberinternal {%if( %1<%numitems( @%2), @lowestmemberinternal( %eval( %1+1), %2, %min( %3, %item( @%2, %1))), %min( %3, %item( @%2, %1)))}
#function lowestmember {%if( %numitems( @%1)>1, @lowestmemberinternal( 1, %1, 0), %item( @%1, 1))}
#function highestmemberinternal {%if( %1<%numitems( @%2), @highestmemberinternal( %eval( %1+1), %2, %max( %3, %item( @%2, %1))), %max( %3, %item( @%2, %1)))}
#function highestmember {%if( %numitems( @%1)>1, @highestmemberinternal( 1, %1, 0), %item( @%1, 1))}

#show @highestmember(numericaltest)
15
#show @lowestmember(numericaltest)
0

no matter what #'s i put in there the lowest will always be 0
Any ideas?
_________________
http://www.Aardwolf.com
Reply with quote
Vijilante
SubAdmin


Joined: 18 Nov 2001
Posts: 5182

PostPosted: Mon Nov 07, 2005 9:37 pm   
 
Lowest of a list... %min(%replace(@List,"|",","))
Highest of a list... %max(%replace(@List,"|",","))

Ahh, zScript is sometimes just that easy.
_________________
The only good questions are the ones we have never answered before.
Search the Forums
Reply with quote
Vitae
Enchanter


Joined: 17 Jun 2005
Posts: 673
Location: New York

PostPosted: Tue Nov 08, 2005 4:11 pm   
 
Well hell, if yer just gonna make things EASY for us ... Twisted Evil
Gonna have a related question about this posted on the General Board later today if I can't figure it out (which i bet i won't)
_________________
http://www.Aardwolf.com
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » Finished MUD Scripts 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