Array Tool

Several functions that allow you to manipulate and process arrays in MUSHCode.

Author: Licenser@M*U*S*H
Category: Functions
Commands: @create, @lock, @set.
Features: #lambda.
Compatibility: PennMUSH.

Instructions

Copy and paste the below code into a compatible MUSH or MUX.

MUSHCode for Array Tool

@create Array Tool
@lock Array Tool==me
@set Array Tool=!NO_COMMAND
&COPYRIGHT Array Tool=Copyright 2007, Heinz N. 'Licenser' Gies %RThis program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You can find a copy at your closest computer with internet connection (http://gnu.org)
&HELP_ARRAYS Array Tool=First, what is an array. It's pretty simple, it's just like a list (like a list of dbrefs: #0 #1 #2). The difference is lists do have a separator char (in the example that would be a space, it also can be | or ^ or about any other character).%rArrays DON'T! That means they are handy for storing strings in them without worrying about removing characters or things like that.%rThat also makes it pretty easier to store an array in an array in an array (like representing a game board with 5 rows and 5 columns you could have an array which holds 5 arrays which have 5 fields, resulting in exactly that a 5 times 5 field). You are not limited to having only two dimensions you can stack them as often as you want!%rTo work with them there are a few functions that allow you to manipulate and process arrays in nearly the same fashion as lists (well not all that many as in help list functions but still the most important are there).%rIndexes are given in numbers. The first element of an array as the index 1. if an array contains another array you can access the 3rd element of the 5th 'sub array' via the index 1 5 (so like walking down the array the firs number gets in the first array, the second number in the second and so on)%R%R(See +ahelp array functions)
&HELP_ARRAY_FUNCTIONS Array Tool=Here a list of functions available for arrays:%R%Ranew([ansi(hc,<length>)] [ansi(hx,\[<legth of n'th subarray> ...\])]) - Creates a new array with the given number of dimensions. anew(2 2) crates an array that holds two arrays with two empty elements each.%R%Rasize([ansi(hc,<array>)]) - returns the lengh of the array.%R%Raget([ansi(hc,<array>)],[ansi(hc,<index>)]) - gets an element from an array, if index is more then one number like aget(...,1 2) it will get the second element of the first 'sub' array.%R%Raset([ansi(hc,<array>)],[ansi(hc,<index>)],[ansi(hc,<new value>)]) - changes the content of an array element - the index is given in the same way as in aget().%R%Radelete([ansi(hc,<array>)],[ansi(hc,<index>)]) - deletes an element of an array, the index is given the same way as in aget(). It causes the elements 'right' to the deleted element to shift, so their index will lower, if you've an array whit 5 elements , so the indexes 1 2 3 4 5 and delete 3 index 4 and 5 will shift left becoming 3 and 4.%R%Ramap([ansi(hc,<object>/<function>)],[ansi(hc,<array>)]) - this functions maps an array, it applies object/function to every element of the array and replaces it with the result of that function call, you can use #lambda! The first argument passed will be the element of the array and the second it's index. (it works the same way as 'help map')%R%Rafilter([ansi(hc,<object>/<function>)],[ansi(hc,<array>)]) - this function filters an array, aka it removes all objects for that object/function returns not a true value (aka 0, en empty string or #-*) you can use #lambda! The first argument passed will be the element of the array and the second it's index. (it works just the same way as 'help filter')
&FN Array Tool=
&FN`APPEND Array Tool=regedit(%0,^(.*?)\\|(.*)$,$1 [strlen(%1)]|$2%1)
&FN`DELETE Array Tool=if(gt(words(%1),1),u(fn`set,%0,first(%1),u(fn`del,u(fn`get,%0,first(%1)),rest(%1),%2)),if(gt(%1,0),if(gt(%1,setr(x,words(setr(p,squish(first(%0,|)))))),#-1 INDEX OUT OF BOUND,[ldelete(%qp,%1)]|[left(setr(d,rest(%0,|)),lmath(add,0 [extract(%qp,1,sub(%1,1))]))][right(%qd,lmath(add,0 [extract(%qp,add(%1,1),%qx)]))]),#-1 INDEX MUST BE POSITIVE INTEGER))
&FN`EACH Array Tool=u(fn`_each,first(%1,|),rest(%1,|),%0,1)
&FN`FILTER Array Tool=trim(u(fn`_filter,|,first(%1,|),rest(%1,|),%0,1),%B,l)
&FN`GET Array Tool=if(gt(words(%1),1),u(fn`get,u(fn`get,%0,first(%1)),rest(%1)),u(fn`_get,first(%0,|),rest(%0,|),%1))
&FN`GET`TESTS Array Tool=
&FN`GET`TESTS`1 Array Tool=
&FN`GET`TESTS`1`IN Array Tool=1|a,1
&FN`GET`TESTS`1`OUT Array Tool=a
&FN`GET`TESTS`2 Array Tool=
&FN`GET`TESTS`2`IN Array Tool=1 2|abb,2
&FN`GET`TESTS`2`OUT Array Tool=bb
&FN`GET`TESTS`3 Array Tool=
&FN`GET`TESTS`3`IN Array Tool=1 2 2|abbcc,2
&FN`GET`TESTS`3`OUT Array Tool=bb
&FN`GET`TESTS`4 Array Tool=
&FN`GET`TESTS`4`IN Array Tool=6|wobble,1
&FN`GET`TESTS`4`OUT Array Tool=wobble
&FN`GET`TESTS`5 Array Tool=
&FN`GET`TESTS`5`IN Array Tool=3 3|1|a1|b,1 1
&FN`GET`TESTS`5`OUT Array Tool=a
&FN`GET`TESTS`6 Array Tool=
&FN`GET`TESTS`6`IN Array Tool=3 3|1|a1|b,2 1
&FN`GET`TESTS`6`OUT Array Tool=b
&FN`MAP Array Tool=trim(u(fn`_map,|,first(%1,|),rest(%1,|),%0,1),%B,l)
&FN`MAP`TESTS Array Tool=
&FN`MAP`TESTS`1 Array Tool=
&FN`MAP`TESTS`1`IN Array Tool=1 1 1|000,#lambda/%%1
&FN`MAP`TESTS`1`OUT Array Tool=1 1 1|123
&FN`MAP`TESTS`2 Array Tool=
&FN`MAP`TESTS`2`IN Array Tool=1 1 1|123,#lambda/mul(%%0\,%%0)
&FN`MAP`TESTS`2`OUT Array Tool=1 1 1|149
&FN`MAP2 Array Tool=fold(#lambda/u(fn`append,%%0,objeval(%#,ulambda([decompose(%0)],aget(%1,%%1),%%1))),lnum(1,words(first(%1,|))),|)
&FN`NEW Array Tool=trim(if(gt(words(%0),1),setq(a,trim(repeat(%B0,first(%0)))|)[iter(lnum(1,first(%0)),setq(a,aset(%qa,##,u(fn`new,rest(%0)))))]%qa,trim(repeat(%B0,%0))|))
&FN`NEW`TESTS Array Tool=
&FN`NEW`TESTS`1 Array Tool=
&FN`NEW`TESTS`1`IN Array Tool=2 2
&FN`NEW`TESTS`1`OUT Array Tool=4 4|0 0|0 0|
&FN`NEW`TESTS`2 Array Tool=
&FN`NEW`TESTS`2`IN Array Tool=5
&FN`NEW`TESTS`2`OUT Array Tool=0 0 0 0 0|
&FN`SET Array Tool=if(gt(words(%1),1),u(fn`set,%0,first(%1),u(fn`set,u(fn`get,%0,first(%1)),rest(%1),%2)),if(gt(%1,0),if(gt(%1,setr(x,words(setr(p,squish(first(%0,|)))))),trim(%qp [strlen(%2)])|[rest(%0,|)]%2,[replace(%qp,%1,strlen(%2))]|[left(setr(d,rest(%0,|)),lmath(add,0 [extract(%qp,1,sub(%1,1))]))]%2[right(%qd,lmath(add,0 [extract(%qp,add(%1,1),%qx)]))]),#-1 INDEX MUST BE POSITIVE INTEGER))
&FN`SET`TESTS Array Tool=
&FN`SET`TESTS`1 Array Tool=
&FN`SET`TESTS`1`IN Array Tool=1|a,1,b
&FN`SET`TESTS`1`OUT Array Tool=1|b
&FN`SET`TESTS`2 Array Tool=
&FN`SET`TESTS`2`IN Array Tool=1 2|abb,1,cc
&FN`SET`TESTS`2`OUT Array Tool=2 2|ccbb
&FN`SET`TESTS`3 Array Tool=
&FN`SET`TESTS`3`IN Array Tool=1 2 2|abbcc,2,gibble
&FN`SET`TESTS`3`OUT Array Tool=1 6 2|agibblecc
&FN`SET`TESTS`4 Array Tool=
&FN`SET`TESTS`4`IN Array Tool=1|a,3,bla
&FN`SET`TESTS`4`OUT Array Tool=1 3|abla
&FN`SET`TESTS`5 Array Tool=
&FN`SET`TESTS`5`IN Array Tool=3 3|1|a1|b,1 1,y
&FN`SET`TESTS`5`OUT Array Tool=3 3|1|y1|b
&FN`SET`TESTS`6 Array Tool=
&FN`SET`TESTS`6`IN Array Tool=3 3|1|a1|x,2 1,x
&FN`SET`TESTS`6`OUT Array Tool=3 3|1|a1|x
&FN`SIZE Array Tool=words(first(%0,|))
&FN`_EACH Array Tool=if(words(%1),objeval(%#,ulambda(%2,mid(%1,0,first(%0)),%3)))[u(fn`_each,rest(%0),mid(%1,first(%0),1000000000),%2,add(%3,1)))
&FN`_FILTER Array Tool=if(words(%1),u(fn`_filter,if(objeval(%#,ulambda(%3,mid(%2,0,first(%1)),%4)),u(fn`append,%0,mid(%2,0,first(%1)),%0),rest(%1),mid(%2,first(%1),1000000000),%3,add(%4,1)),%0)
&FN`_GET Array Tool=if(gt(%2,0),[if(lt(extract(%0,%2),1),,mid(%1,lmath(add,0 [extract(%0,1,sub(%2,1))]),extract(%0,%2)))],#-1 INDEX MUST BE POSITIVE INTEGER)
&FN`_MAP Array Tool=if(words(%1),u(fn`_map,u(fn`append,%0,objeval(%#,ulambda(%3,mid(%2,0,first(%1)),%4))),rest(%1),mid(%2,first(%1),1000000000),%3,add(%4,1)),%0)
&FN`_SET Array Tool=if(gt(%2,0),if(gt(%2,words(%0)),trim(%0 [strlen(%3)])|%1%3,[replace(%0,%2,strlen(%3))]|[left(%1,lmath(add,0 [extract(%0,1,sub(%2,1))]))]%3[right(%1,lmath(add,0 [extract(%0,add(%3,1),words(%0))]))]),#-1 INDEX MUST BE POSITIVE INTEGER)
&STARTUP Array Tool=@fun/preserve asize=#10604,fn`size,1,1;@fun/preserve aget=#10604,fn`get,2,2;@fun/preserve aset=#10604,fn`set,3,3;@fun/preserve anew=#10604,fn`new,1,1;@fun amap=#10604,fn`map,2,2;@fun afilter=#10604,fn`filter,2,2;@fun/preserve adelete=#10604,fn`delete,2,2;@fun aeach=#10604,fn`each,2,2
@set Array Tool/STARTUP=no_command prefixmatch
&VX Array Tool=3|6 5 1|hallo world!

th Edit the STARTUP attribute