Working with lists

Working with lists javelin Sun, 2012-02-12 22:03

Working with lists

How can I explode a string into a list of characters?

How can I explode a string into a list of characters? javelin Sun, 2012-02-12 22:12

How can I explode a string into a list of characters?

trim(edit(mystring,,%b))

2001-Apr-01 4:54pm dunemush

How can I get every other item of a list (Or every 3rd, 4th, etc.)?

How can I get every other item of a list (Or every 3rd, 4th, etc.)? javelin Sun, 2012-02-12 22:17

How can I get every other item of a list (Or every 3rd, 4th, etc.)?

Up to every 10th item can be gotten very easily, with the help of step().

For example:

&every_other foo=%1

think step(foo/every_other, a b c d e f, 2) 

=> b d f

If you want every other element starting with the first, use
%0 instead of %1.

Alternatively, you can use elements() and build the list of
positions using lnum() and vmul().

For example:

think elements(a b c d e f, vmul(lnum(0, 6), 2))

2001-Nov-13 6:44pm shawnw

How can I remove all spaces from a list?

How can I remove all spaces from a list? javelin Sun, 2012-02-12 22:11

How can I remove all spaces from a list?

edit(list of elements,%b,)

2001-Apr-01 4:52pm dunemush

How do I convert a list into a list of arguments?

How do I convert a list into a list of arguments? javelin Sun, 2012-02-12 22:20

How can I convert a standard space-separated list of integer values into a list of arguments that can be passed to a function that accepts an indeterminate number arguments, such as add() or max()?

2006-Jun-29 12:29pm misfit815

The answer is in Section 14.5 of Amberyl\'s MUSH manual. The use of fold() is necessary.

2006-Jun-29 1:23pm misfit815

If you want to pass a space-separated list of numbers to a math function, lmath() can be used instead of fold().

2007-Jan-29 12:05pm shawnw

How do I get a list of all the characters/letters the mush accepts?

How do I get a list of all the characters/letters the mush accepts? javelin Sun, 2012-02-12 22:19

How do I get a list of all the characters/letters the mush accepts?

Many mushes can work with non-ASCII accented letters and other special characters. The @nameaccent attribute, and the accent(), chr() and ord() functions can be used to generate some of them in softcode, and you can send others through your client if you can type them, or cut&paste. But what about seeing exactly what characters the mush will handle in a better way than trial and error?

With the regraball() function, you can generate lists of every character the mush considers "printable" and thus accepts, or the characters that meet stricter restrictions, like "every lower-case letter".

To do this, call regraball() like so:

regraball(iter(lnum(1, 255), chr(##)), ^\\[\\[:FOO:\\]\\]$)

FOO is one of the following classes:

 upper, for upper-case letters,
 lower, for lower-case letters,
 alpha, for letters,
 digit, for digits,
 alnum, for letters and digits,
 word, for letters, digits and _,
 xdigit, for digits in base-16 numbers,
 ascii, for characters in the ASCII set only,
 punct, for punctuation characters,
 space, for white-space characters
 graph, for non-white-space characters,
 print, for all characters

There\'s also cntrl, but none of the characters that can match it can be
generated by chr().

If you want to get just characters that also fall into a certain range (Like, from A to Z), you can fiddle with the lnum() bit as needed.

2004-Jul-24 10:50am shawnw

Here\'s a line of code you can just paste to make it list all the printable characters:

th edit(iter(lnum(1,255), switch(chr(##), #-1*,,#$)),%b,)

2003-Apr-23 7:28am pmak

I have a list of attributes named FOO1, FOO2,...FOO100. How do I sort them so the numbers are in the right order?

I have a list of attributes named FOO1, FOO2,...FOO100. How do I sort them so the numbers are in the right order? javelin Sun, 2012-02-12 22:09

I have a list of attributes named FOO1, FOO2,...FOO100. How do I sort them so the numbers are in the right order?

The default PennMUSH sort() is lexicographic, which means that FOO2 will be sorted after FOO100.
For simple lists of numbers, you could do sort(1 2 ... 100,n) and get a numeric sort, but that won\'t work for attribute names with text before the numbers. Here\'s a few things that will:

&HELPER obj=[sub(after(%0,FOO),after(%1,FOO))]

sortby(obj/HELPER,FOO1 FOO2 ... FOO100)


&NUMSORT obj=[sort(%0,n)]

munge(obj/numsort, edit(FOO1 FOO2 ...,FOO,), FOO1 FOO2 ...)

The first version works by comparing the numbers after "FOO" in a given pair of attribute names, and returns a negative number when %0 should be before %1, a positive number when %1 should be before %0, and 0 when they\'re the same. This is exactly what sortby() needs to sort the list.
The second version uses edit() to build a list of just the numerical parts of the attributes, and munge sorts this list numerically and then returns the corresponding elements out of the original list.

2001-Mar-28 10:02am dunemush

Or, you could have named them as FOO001, FOO002, ..., FOO100 in the first place, thus allowing you to sort lexographically. You can generate zero-padded numbers this way:

> think iter(lnum(1, 100), rjust(##, 3, 0))

001 002 003 ... 100

2003-Apr-23 7:32am pmak

List X contains element W. How do I get the corresponding element from list Y of the same length?

List X contains element W. How do I get the corresponding element from list Y of the same length? javelin Sun, 2012-02-12 22:05

List X contains element W. How do I get the corresponding element from list Y of the same length?

Take your pick:

extract(Y,member(X,W),1)
elements(Y,member(X,W))

2001-Mar-28 9:55am dunemush

This also works:

  
[set(me,uh_temp:W)][munge(uh_temp,X,Y)]

This can be useful when W is produced by a u() call w/o arguments (you can get rid of the set() and just use the name of the u()\\\\\\\\\\\\\\\'d attribute in place of uh_temp).

2001-Mar-28 11:05am popiel

What\\\'s the difference between match(), member() and strmatch()?

What\\\'s the difference between match(), member() and strmatch()? javelin Sun, 2012-02-12 22:15

What\\\'s the difference between match(), member() and strmatch()?

match(list, wildcard pattern[, delimiter])
member(list, element[, delimiter])
strmatch(string, wildcardpattern)

match() and member() are list functions that look for a matching element in a list and return its position in the list, or 0 if it\\\'s not found. member() does case-sensitive exact matches, which makes it good for things like dbrefs. match() is case-insensitive, and does wildcard matching (* and ?), so it can be used to find things like the first element of the list starting with the letter F (or f).

strmatch(), on the other hand, is a string function. Instead of treating its first argument as a list to be broken up into individual elements to look at, it compares the entire argument at once against the wildcard pattern, and returns 1 if it matches, 0 if not.

2001-Oct-22 6:52pm shawnw

Why is this code that uses iter() adding spaces I don\\\'t want?

Why is this code that uses iter() adding spaces I don\\\'t want? javelin Sun, 2012-02-12 22:13

Why is this code that uses iter() adding spaces I don\\\'t want?

The spaces are probably coming from the default output seperator of iter(), which happens to be a space. An empty fourth argument to iter() will eliminate the output seperator.

For example:

iter(#1 #2 #3, complex formatting code, %b, )

2001-Jun-11 8:16pm shawnw