Array Functions in MR-lib

© 2012, Martin Rinehart


copy = array_copy( original );

Make a one-dimensional shallow copy of the original array.


copy_2d = array_2d_copy( original );

Makes a shallow copy of a two-dimensional original.

array_count( number )

array_count( number );

Returns [0, 1, 2, ..., 9] for array_count( 10 ).


array_delete( array, index );

Delete array element. Deletes within the array. (No return.)


new_array = array_fill( value, count );

Fill new array with count occurences of value.


index = array_index_of( array, value );

Return index in array of first occurence of value or -1 if not found.


indexes = array_indexes_of( array, value );

Returns an array of the indexes of value in array or, if none found, an empty array.


/* modifies perhaps_short_array */ array_pad( perhaps_short_arr, value, length );

Pads a (possibly short) array with copies of value to the length specified. Does nothing if the original array is equal to or longer than the specified length. Uses array_fill() to create a new array of the specified length if the array does not exist.

array_shuffle( number )

indices = array_shuffle( number );

Returns array of indices 0 through number - 1 in 'random' order.


boolean = array_starts_with( array, start_array );

Returns true if array starts with the elements of start_array.


values_array = array_subscript( array, subscripts_array );

Returns a new array of the values in another array at the positions of the subscripts in a subscripts array.

Example: if subscripts_array = [ 2, 3 ]; this would return an array of the third and fourth elements of array. ([ array[2], array[3] ]).


result_array = array_2d_subscript( array_2d, subscripts_array );

Returns an array_2d given subscripts into another array_2d. The subscripts refer to the columns in the original array_2d.


var array_2d = [
    [  0,        1,     2  ,    3     ],
    [ 'a',      'b',   'c' ,   'd'    ],
    [ 'mouse', 'cat', 'dog', 'python' ]

var result = array_2d_subscript( array_2d, [1, 2] );

/* result = [
    [   1,     2   ],
    [  'b',   'c'  ],
    [ 'cat', 'dog' ]


new_array = flatten( old_array );

Creates a new array in which elements are the values in the original array or, if a value is an array, each element of the array. Note this is not recursive. It works to only one level.


list = get_column( array_2d, column_number );

Returns an array that is a column in a two-dimensional array. (Simple case of array_2d_subscript().)

Feedback: MartinRinehart at gmail dot com

# # #