MultiScript - Array functions
Array is an internal data type in MultiScript.
An array can contain multiple values of type string, number or array.
The following is a list of supported array functions:
| array initialization | Initialize an array with values when declared. |
| arrayCount | Count the items in an array. |
| arrayAdd | Add an item to an array. |
| arrayRemove | Remove an item from an array. |
| arrayFind | Find a string item in an array. |
| arrayIFind | Find a string item in an array, Ignoring case differences. |
| ArraySort | Sort the items in an array. |
| ArrayToString | Concatenate all string in an array to a string. |
| StrLines2Array | Transform a string with multiple lines into an array of lines. |
| StrLinesArray2String | Transform an array of strings into a multi-line string. |
| StrTokenize2Array | Tokenize a string into an array. |
| operator [] | Get or Set an array item at a specified index position. |
| ArrayReverse | Return a reversed copy of an array. |
| ArraySlice | Return a portion of an array as a new array. |
See also
Initialize Array
You can initialize an array when it is declared.
@var $arr[] = {<str>|<num>, <str>|<num>, ..., ... };
Parameters
- <str>|<num>
- String or Number values to insert into the array
Return value
An array with values inserted into it
Example
@var $arr[] = {"String1", "String2", "String3"};
@var $len = arrayCount($arr);
// $len is 3.
arrayCount
Count the items in an array.
<num> arrayCount( <array> input );
Parameters
- input
- Array to count items in
Return value
The number of items that are currently in the array
Example
See 'Initialize Array', above
arrayAdd
Add an item to an array.
<none> arrayAdd( <array> input, <str>/<num> value );
Parameters
- input
- Array to add value into
- value
- String or Number value that should be added to the array
Return value
Nothing
arrayRemove
Remove an item from an array.
<none> arrayRemove( <array> input, <num> index );
Parameters
- input
- Array to remove item from
- index
- The position in the array from which to remove the item, beginning at index 0
Return value
Nothing
arrayFind
Search an array for a string value (case-sensitive).
<num> arrayFind( <array> input, <str> string );
Parameters
- input
- Array to be searched for the string
- string
- The string item to look for
Return value
Index position of the found string, or -1 if the string is not found
arrayIFind
Find a string value in an array, ignoring case differences.
<num> arrayIFind( <array> input, <str> string );
Parameters
- input
- Array to be searched for the string
- string
- The string item to look for
Return value
Index position of the found string, or -1 if string is not found
ArraySort
Sort the items in an array.
<arr> ArraySort( <arr> arr );
Parameters
- arr
- Array that should be sorted
Return value
A new array that is sorted
Example
@var $arr[] = {3, 5, 2};
@var $arrNew = ArraySort( $arr );
// arrNew = 2,3,5
@var $arr[] = {"DD", "CC", "AA"};
@var $arrNew = ArraySort( $arr );
// arrNew = "AA", "CC", "DD"
ArrayToString
Join all string in an array to a string.
<str> ArraySort( <arr> arr, <str> delimiter, <num> escape);
Parameters
- arr
- Array with strings
- delimiter
- What delimiter to use between the string, Eg " , " if you want space then comma then a space
- aescaper
- Set to 1 if you want all the string to have " around them. like if they are paths.
Return value
A string
Example
@var $arr[] = { 'c:\MyFile1.txt', 'c:\MyFile2.txt', 'c:\MyFile3.txt'};
@var $arrNew = ArrayToString( $arr, " , ", 0 );
// arrNew = c:\MyFile1.txt , c:\MyFile2.txt , c:\MyFile3.txt
@var $arr[] = { 'c:\MyFile1.txt', 'c:\MyFile2.txt', 'c:\MyFile3.txt'};
@var $arrNew = ArrayToString( $arr, " , ", 1 );
// arrNew = "c:\MyFile1.txt" , "c:\MyFile2.txt" , "c:\MyFile3.txt""
StrLines2Array
Transform a string with multiple lines into an array.
<array> StrLines2Array( <str> input );
Parameters
- input
- Multi-line string (all forms of newline are supported)
Return value
An array where the strings in input have been split at the line breaks
StrLinesArray2String
Transform an array of strings into a multi-line string.
<str> StrLinesArray2String( <array> input, [<num> eol] );
Parameters
- input
- Array of strings
- eol (Optional)
- what kind of line break to use:
0 = CRLF (Windows) (this is the default if none is specified)
1 = LF (Linux)
2 = CR (Mac)
Return value
A multi-line string where the strings in the array input have been appended together with line breaks inserted between the strings; the line break type is determined by the eol value
Example
@var $arr[] = {"String1", "String2", "String3"};
@var $strLines = StrLinesArray2String( $arr );
@var $strLinesLF = StrLinesArray2String( $arr, 1 );
StrTokenize2Array
Tokenize a string into an array, splitting it at specified delimiters.
<array> StrTokenize2Array( <str> input, <str> delimiter );
Parameters
- input
- String to tokenize
- delimiter
- Characters at which to split the string
Return value
An array of strings based on the string input after it is split. It is split wherever there are characters that match those in delimiter. The delimiter character(s) will not appear in the new array (ie., they will be lost)
operator [ ]
Index operator for setting or getting a value from an array.
<any> operator [<num>] <any>;
Parameters
- [<num>]
- The index position to set or get a value, beginning at index 0
Return value
The item at the selected index position
Example
@var $arr[] = {"Str1", "Str2", "Str3"};
$arr[1] = "Frog";
// string now contain the strings "Str1","Frog","Str3";
@var $str = $var[2];
// $str now contain the value "Str3"
ArrayReverse
Reverse the order of items in an array.
<arr> ArrayReverse( <arr> input );
Parameters
- input
- The array to reverse
Return value
A new array with the items in reversed order. The original array is not modified.
Example
@var $arr[] = {1, 2, 3, 4, 5};
@var $rev = ArrayReverse( $arr );
// $rev == {5, 4, 3, 2, 1}
@var $arr[] = {"Alpha", "Beta", "Gamma"};
@var $rev = ArrayReverse( $arr );
// $rev == {"Gamma", "Beta", "Alpha"}
ArraySlice
Extract a portion of an array into a new array.
<arr> ArraySlice( <arr> input, <num> start [, <num> end] );
Parameters
- input
- The source array
- start
- The index of the first item to include (0-based). Negative values count from the end: -1 is the last item.
- end
- Optional. The index one past the last item to include. If omitted or -1, the slice runs to the end of the array. Negative values count from the end.
Return value
A new array containing the selected items. The original array is not modified. Returns an empty array if the range is invalid.
Example
@var $arr[] = {"A", "B", "C", "D", "E"};
@var $s1 = ArraySlice( $arr, 1 ); // {"B","C","D","E"} -- from index 1 to end
@var $s2 = ArraySlice( $arr, 1, 3 ); // {"B","C"} -- index 1 and 2 (end exclusive)
@var $s3 = ArraySlice( $arr, 0, 2 ); // {"A","B"}
@var $s4 = ArraySlice( $arr, -2 ); // {"D","E"} -- last two items