 |
orphean Apprentice

Joined: 21 Oct 2008 Posts: 147 Location: Olympia, WA
|
Posted: Mon Oct 19, 2009 1:23 am
List Rotation Functions |
Another recent thread made me start thinking about rotating string lists in Cmud and how useful this capability is in many situations. To that end, I ended up creating a couple of small little functions called rotateleft and rotateright which take a string list and rotates it one step in either direction.
It seemed too small to share on the package library so I figured I'd share them here. These are hardly going to light the cmud world on fire as they are very simple functions, but, maybe they'll prove of use to someone.
rotateleft
| Code: |
<func name="rotateleft" type="StringList" id="234">
<value>#return %additem(%pop($strlist), $strlist)</value>
<arglist>$strlist</arglist>
<notes>Rotates each item in a string list to the left one slot and returns the new list.</notes>
</func> |
rotateright
| Code: |
<func name="rotateright" type="StringList" id="238">
<value>#return %push(%item($strlist, %numitems($strlist)), %delnitem($strlist, %numitems($strlist)))</value>
<arglist>$strlist</arglist>
<notes>Rotates each item in a string list to the right one slot and returns the new list.</notes>
</func> |
And here's a quick example session:
| Code: |
#var testlist "A|B|C"
#show @testlist
> A|B|C
#var testlist @rotateleft(@testlist)
#show @testlist
> B|C|A
#var testlist @rotateleft(@testlist)
#show @testlist
> C|A|B
#var testlist @rotateleft(@testlist)
#show @testlist
> A|B|C
#var testlist @rotateright(@testlist)
#show @testlist
> C|A|B
etc... |
|
|
|
|
 |
orphean Apprentice

Joined: 21 Oct 2008 Posts: 147 Location: Olympia, WA
|
Posted: Mon Oct 19, 2009 7:06 am |
Well I kept going and ended up with a small library that provides supplementary string list functions for Cmud. This seems useful enough that I uploaded it to the package library with the name ListFunctions (super creative name) for <All> muds.
Documentation for each function is included in the function's note. Here's a copy-paste of the package's description with a list of the included functions:
| Code: |
A supplemental set of functions that operate on string lists.
CMud includes many list functions builtin but there is a set of common list operations that are useful to have around but not included by default. This package attempts to address this. These functions are loosely based on a subset of PennMUSH softcode's list functions.
concatlist() - Concatenates two lists together.
first() - Returns the first item in a list.
last() - Returns the last item in a list.
rest() - Returns every item but the first in a list.
reverselist() - Reverses the order of items in a list.
rotateleft() - Moves each item in the list left one slot.
rotateright() - Moves each item in the list right one slot.
setunion() - Returns the union of two lists.
setdiff() - Returns the difference of two lists.
setinter() - Returns the intersection of two lists.
grab()
graball()
regrab()
regraball() - Search a list for a pattern and return either a single result, or a list of all matches. The re* functions use regular expressions for the pattern.
match()
rematch() - Like the grab* functions but return a 1 for a match or a 0 for no match. rematch uses regular expressions. |
|
|
|
|
 |
|
|
|
|
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
|
|