Skip to content
eaxs edited this page Jan 4, 2012 · 2 revisions

Allows the creation and manipulation of pseudo array objects. Arrays are lists of items, such as fruits: Apple, Banana, Orange etc..


Config variables

This module has no config variables


Method: new

Creates a new array. If the provided variable name already exists, it will be overwritten. The index of the array is stored in the variable [array_name]_idx. The index of an empty array is -1.

Parameter Options:

Array.new    string    required    The variable name of the array

Example:

This example creates a new array object.

#$e# K2 Array.new "fruits";

Method: add

Adds data to an existing array. You can add up to 10 new elements (ending at param option -d9) per call to the array. The data will be appended at the end of the provided array.

Parameter Options:

Array.add    object    required    The target array
-d0          mixed     required    The data to append
-d1          mixed     optional    The data to append
-d2          mixed     optional    The data to append
-d3          mixed     optional    The data to append
etc....

Example:

This example adds a bunch of fruits to an array called “fruits”.

#$e# K2 Array.new "fruits";
#$e# K2 Array.add fruits -d0 Apple -d1 Banana -d2 Orange;

// Will say: Apple
#<# #fruits_0#
// Will say: Banana 
#<# #fruits_1#
// Will say: Orange
#<# #fruits_2#
// Will say: 2 (because the first element index starts with 0)
#<# #fruits_idx#

Method: explode

Creates an array from a string. The string will be separated by the provided delimeter option. Note that the original string will still exist after this operation. Very important: The target string or the delimeter must not contain any commas, semi-colons, or brackets. I recommend using the & symbol instead.

Parameter Options:

Array.explode   string    required    The target string
-d              string    required    The delimeter string by which to separate
-v              string    required    Callback var which holds the resulting array

Example:

This example will convert a string of fruits into an array called “fruits”.


#$# fruits_string "Apple&Banana&Orange";
#$e# K2 Array.explode #fruits_string# -d "&" -v fruits

// Will say: Apple
#<# #fruits_0#
// Will say: Banana 
#<# #fruits_1#
// Will say: Orange
#<# #fruits_2#
// Will say: 2 (because the first element index starts with 0)
#<# #fruits_idx#

Method: implode

Turns an array into a string. The array elements will be “glued” together by the glue option. The original array will still exist after this operation. Very important: The glue must not be a comma, semi-colon, or bracket

Parameter Options:

Array.implode    object    required    The target array
-v               string    required    Callback var which holds the resulting string
-g               string    optional    The glue by which to connect the array elements

Example:

This example will turn the array “fruits” into a string variable “text”.

#$e# K2 Array.new "fruits";
#$e# K2 Array.add fruits -d0 Apple -d1 Banana -d2 Orange;
#$e# K2 Array.implode fruits -v text -g "-";

// Will say: Apple-Banana-Orange
#<# #text#;

Method: each

Loops through the elements of an array and executes the provided code for each element. You can use el as a reference to the currently looped element and i as a reference to the current iterator. Note that this method may be unstable if the code you’re trying to execute is too “complicated”. If you need to run complex code on each element, it is recommended to loop through the array manually.

Parameter Options:

Array.each   object    required    The array to loop through
-do          string    required    The code to execute on each element

Example:

This example will echo out each array element one after another.


#$e# K2 Array.new "fruits";
#$e# K2 Array.add fruits -d0 Apple -d1 Banana -d2 Orange;

#$e# K2 Array.each fruits -do "#<# index: %i% - %el%";
// Will output:
// index: 0 - Apple
// index: 1 - Banana
// index: 2 - Orange

Method: find

Finds a value in a given array and returns the index if found. If no key is found, -1 is returned.

Parameter Options:

Array.find    object    required    The array to search
-s            mixed     required    The value to find
-v            string    required    The callback var which holds the index of the search result

Example:

This example will search the value “Banana” in the “fruits” array and will return the index value.


#$e# K2 Array.new "fruits";
#$e# K2 Array.add fruits -d0 Apple -d1 Banana -d2 Orange;

#$e# K2 Array.find fruits -s "Banana" -v fruit_index;

// Will say: 1
#<# #fruit_index#;

Method: rmkey

Added in version 1.1.0. Removes an element from the given array. Note that the array will be re-built and that key-value association will be changed.

Parameter Options:

Array.rmkey    object    required    The array from which to remove
-k             int       required    The key of the element to remove

Examples:

This example will remove “Apple” from the Array.


#$e# K2 Array.new "fruits";
#$e# K2 Array.add fruits -d0 Apple -d1 Banana -d2 Orange;

#$e# K2 Array.rmkey fruits -k 0;

#$e# K2 Array.each fruits -do "#<# index: %i% - %el%";
// Will output:
// index: 0 - Banana
// index: 1 - Orange

If you dont know the key of the element, you can use “Array.find” first.


#$e# K2 Array.new "fruits";
#$e# K2 Array.add fruits -d0 Apple -d1 Banana -d2 Orange;

#$e# K2 Array.find fruits -s "Apple" -v apple_key;
#$e# K2 Array.rmkey fruits -k #apple_key#;

Method: empty

Added in version 1.1.0. Empties an array with all its elements and resets the index to -1.

Parameter Options:

Array.empty    object    required    The array to empty

Example:

This example will empty the fruits array.


#$e# K2 Array.new "fruits";
#$e# K2 Array.add fruits -d0 Apple -d1 Banana -d2 Orange;

#$e# K2 Array.empty fruits;

#$e# K2 Array.each fruits -do "#<# index: %i% - %el%";
// Will output nothing

Method: copy

Added in version 1.1.0. Creates a copy of an array.

Parameter Options:

Array.copy    object    required    The array to copy
-v            string    required    The callback var which contains the copy

Example:

This example will copy the fruits array.


#$e# K2 Array.new "fruits";
#$e# K2 Array.add fruits -d0 Apple -d1 Banana -d2 Orange;

#$e# K2 Array.copy fruits -v fruits_are_all_the_same;

#$e# K2 Array.each fruits_are_all_the_same -do "#<# index: %i% - %el%";
// Will output:
// index: 0 - Apple
// index: 1 - Banana
// index: 2 - Orange

Method: reverse

Added in version 1.1.0. Reverses the element order of an array. Note that the key-value association will be changed.

Parameter Options:

Array.reverse    object    required    The array to reverse

Example:

This example will reverse the fruits array.


#$e# K2 Array.new "fruits";
#$e# K2 Array.add fruits -d0 Apple -d1 Banana -d2 Orange;

#$e# K2 Array.reverse fruits

#$e# K2 Array.each fruits -do "#<# index: %i% - %el%";
// Will output:
// index: 0 - Orange
// index: 1 - Banana
// index: 2 - Apple

Clone this wiki locally