Core Function Splice
Splice( <array>, <offset>, <length>, <replace_with> ... )
Contents |
Description
Cut out and return a chunk or portion of an array and optionally replace the cut out section with new data.
Parameters
array
The array to use.
offset
The offset within the array to begin splicing at (Index number).
length
Optional; How many splices to make counting up from the offset
If this parameter is not specified the length will be the full extent of the array.
replace_with
Optional; Data to insert at the
Return Value
Success: Returns new array of the spliced data.
Failure: Returns empty array.
Remarks
Works very similar to Perls Splice() function for them who know it.
Example
A simple example with commentary
/* Sputniks's splice() function is used to cut out and return a chunk or portion of an array. The portion that is cut out starts at the OFFSET element of the array and continues for LENGTH elements. If the LENGTH is not specified, it will cut to the end of the array. */ $myNames = array('Jacob', 'Michael', 'Joshua', 'Matthew', 'Ethan', 'Andrew'); $someNames = splice($myNames, 1, 3); println("#################################"); println("First example"); println("Orig array modified:"); printr($myNames); println("New array:"); printr($someNames); println("#################################"); /* Think of the $myNames array as a row of numbered boxes, going from left to right, numbered starting with a zero. The splice() function would cut a chunk out of the $myNames array starting with the element in the #1 position (in this case, Michael) and ending 3 elements later at Matthew. The value of $someNames then becomes ('Michael', 'Joshua', 'Matthew'), and $myNames is shortened to ('Jacob', 'Ethan', 'Andrew'). As an option, you can replace the portion removed with another array by passing it in the REPLACE_WITH argument. */ $myNames = array('Jacob', 'Michael', 'Joshua', 'Matthew', 'Ethan', 'Andrew'); $moreName = array('Daniel', 'William', 'Joseph'); $someNames = splice($myNames, 1, 3, $moreName); println("#################################"); println("Second example"); println("Orig array modified:"); printr($myNames); println("New array:"); printr($someNames); println("#################################"); /* In the above example, the splice() function would cut a chunk out of the $myNames array starting with the element in the #1 position (in this case, Michael and ending 3 elements later at Matthew. It then replaces those names with the contents of the $moreNames array. The value of $someNames then becomes ('Michael', 'Joshua', 'Matthew'), and $myNames is changed to ('Jacob', 'Daniel', 'William', 'Joseph', 'Ethan', 'Andrew'). */
A relatively complex example where elements are removed from an array and replaced and a new array is created from the removed elements.
$Replace = array("ONE", "TWO", "THREE"); $hehe = array("fox", "cow"); $Test = array("Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight"); $Test2 = splice($Test, 1, 3, $Replace, "cat", "dog", $hehe); printr($Test); printr($Test2);
Another simple example
my $array = qw/1 2 3 11 12 10/; my $returnedArray = splice($array, 3, 2, 4..9); print "\$array = ($array[<>])\n"; print "\$returnedArray = ($returnedArray[<>])\n"; // Prints: // $array = (1 2 3 4 5 6 7 8 9 10) // $returnedArray = (11 12)
Splice array up to index 8 (Everything past index 8 is deleted)
// initialize an array my $array = qw/1 2 3 4 5 6 7 8 9 10/; // using a positive OFFSET splice($array, 8); print "\$array = ($array[<>])\n"; // Prints: // $array = (1 2 3 4 5 6 7 8)
Splice array by removing 3 elements from the beginning (using -3 offset)
// initialize an array my $array = qw/1 2 3 4 5 6 7 8 9 10/; // using a positive OFFSET $ret = splice($array, -3); print "\$array = ($array[<>])\n"; print "\$ret = ($ret[<>])\n"; // Prints: // $array = (1 2 3 4 5 6 7) // $ret = (8 9 10)
This example that shows you how to insert a list at the beginning of an array:
my $array = qw/6 7 8 9 10/; splice($array, 0, 0, 1..5); print "\$array = ($array[<>])\n"; // Prints: // $array = (1 2 3 4 5 6 7 8 9 10)
This shows you how to delete a portion of an array from the beginning of the array:
my $array = qw/1 2 3 4 5/; splice($array, 0, 2); print "\$array = ($array[<>])\n"; // Prints: // $array = (3 4 5)