8 January 2013
Array manipulation functions
I have spent a bit of time playing around
with functions to manipulate
arrays.
There are functions that treat an array as a stack
(e.g. pop
and push
); functions
to insert
and remove
an element;
functions to move elements around, etc..
In all these functions, the array (or arrays) will be passed by reference, i.e. its name, rather than a list of its elements, and often modifies the array itself.
The functions haven't been thoroughly tested (there's a framework for a test suite here), and they should have more documentation. I'd like to add some examples. But I don't know whether or not I'll spend any more time on either the functions or this section of my website.
- add() #@ Pop two elements, add them, leave result on top of stack and in supplied variable
- add_end() #@ Add element to end of list
- cleave() #@ Split string into array using SEP as delimiter
- compact() #@ Remove unset elements holding a place in array
- concat() #@ Concatenate two arrays, store in third
- copy() #@ Copy array to new array
- count() #@ Return number of elements in array
- dup() #@ Duplicate top item on stack
- duplicate() #@ Create a copy of an array
- exch() #@ Exchange top two elements on stack
- fib() #@ Add next number in fibonacci series to end of array
- forall() #@ Perform action on each element of the array
- insert() #@ Insert element into array at position
- longest() #@ Find the longest element (most characters)
- max() #@ Find the element with the highest value (text or numeric)
- mean() #@ Average integer elements in array
- merge() #@ Merge 2 arrays
- mergex() #@ Merge 2 arrays including unset elements
- min() #@ Find the element with the lowest value (text or numeric)
- move() #@ Move element to right or left
- pair() #@ Pair corresponding elements of 2 arrays to single element in new array
- peek() #@ Store top element of array in variable, but do not remove it
- peek_end() #@ Get element from end of list
- pop() #@ Remove top element (array[0]) from stack and place in supplied variable
- pop_end() #@ Remove element from end of list and store in supplied variable
- push() #@ Add value[s] to beginning of array
- remove() #@ Remove element from array
- reverse() #@ Reverse order of array
- roll() #@ Rotate top N elements; [N] moves to [0] with optional repeat
- rolr() #@ Rotate top N elements; [0] moves to [N] with optional repeat
- rotate() #@ Move element from one end of the array to the other with optional repeat
- rshove() #@ Add element to end of array and remove first element
- shortest() #@ Find the shortest element (fewest characters)
- shove() #@ Add element to beginning of array and remove last element
- show() #@ Print, or store in variable, all elements of array
- sub() #@ Pop two elements, subtract, leave result on top of stack and in supplied variable
- total() #@ Add all numerical elements of array
Modified 29 Jun 2015