"Sputnik" help  
Sputnik Help
Language Reference - Datatypes - Arrays (Splicing)

Arrays (Splicing)

This is not to be confused with Slicing.

You copy parts out of an array and return them as a new array.

All actions apply to the array and only return VALUES not keys.

Splicing can overlap and return the same item more than once.

If anything else is entered it will return the KEY that it links to.

Extract elements 0 to 3 from an array:

my $array = array(  "Zero", 
                    "One", 
                    "Two", 
                    "Three", 
                    "Four", 
                    "Five", 
                    "Six", 
                    "Seven", 
                    "Eight"
                );
printr($array[0 rr 3]);
// PRINTS
// Array
// (
//     [0] => Zero
//     [1] => One
//     [2] => Two
//     [3] => Three
// )

Extract elements 3 to 0 from an array:

my $array = array(  "Zero", 
                    "One", 
                    "Two", 
                    "Three", 
                    "Four", 
                    "Five", 
                    "Six", 
                    "Seven", 
                    "Eight"
                );
printr($array[3 rr 0]);
// PRINTS
// Array
// (
//     [0] => Three
//     [1] => Two
//     [2] => One
//     [3] => Zero
// )

Extract elements 0 to 3 AND 5 to 6 from an array:

my $array = array(  "Zero", 
                    "One", 
                    "Two", 
                    "Three", 
                    "Four", 
                    "Five", 
                    "Six", 
                    "Seven", 
                    "Eight"
                );
printr($array[0 rr 3, 5 rr 6]);
// PRINTS
// Array
// (
//     [0] => Zero
//     [1] => One
//     [2] => Two
//     [3] => Three
//     [4] => Five
//     [5] => Six
// )

Extract elements 0 to 3 AND 5 to 6 AND 7 from an array:

my $array = array(  "Zero", 
                    "One", 
                    "Two", 
                    "Three", 
                    "Four", 
                    "Five", 
                    "Six", 
                    "Seven", 
                    "Eight"
                );
printr($array[0 rr 3, 5 rr 6, 7]);
// PRINTS
// Array
// (
//     [0] => Zero
//     [1] => One
//     [2] => Two
//     [3] => Three
//     [4] => Five
//     [5] => Six
//     [6] => Seven
// )

Extract elements 0 to 3 AND 5 to 6 AND 7 to 1 from an array:

my $array = array(  "Zero", 
                    "One", 
                    "Two", 
                    "Three", 
                    "Four", 
                    "Five", 
                    "Six", 
                    "Seven", 
                    "Eight"
                );
printr($array[0 rr 3, 5 rr 6, 7 rr 1]);
// PRINTS
// Array
// (
//     [0] => Zero
//     [1] => One
//     [2] => Two
//     [3] => Three
//     [4] => Five
//     [5] => Six
//     [6] => Seven
//     [7] => Six
//     [8] => Five
//     [9] => Four
//     [10] => Three
//     [11] => Two
//     [12] => One
// )

You can even splice in backwards counting such as:

my $array = array(  "Zero", 
                    "One", 
                    "Two", 
                    "Three", 
                    "Four", 
                    "Five", 
                    "Six", 
                    "Seven", 
                    "Eight"
                );
printr($array[3 rr 0, 6 rr 5, 1 rr 7]);
// PRINTS
// Array
// (
//     [0] => Three
//     [1] => Two
//     [2] => One
//     [3] => Zero
//     [4] => Six
//     [5] => Five
//     [6] => One
//     [7] => Two
//     [8] => Three
//     [9] => Four
//     [10] => Five
//     [11] => Six
//     [12] => Seven
// )

Extract a value out a dictionary (For whatever reason):

my $array = array(  "A" => "Zero",
                    "B" => "One",
                    "C" => "Two",
                    "D" => "Three",
                    "E" => "Four",
                    "F" => "Five",
                    "G" => "Six", 
                    "H" => "Seven", 
                    "I" => "Eight"
                );
printr($array['G']);
// PRINTS
// Six

Extract a some of values out a dictionary:

my $array = array(  "A" => "Zero",
                    "B" => "One",
                    "C" => "Two",
                    "D" => "Three",
                    "E" => "Four",
                    "F" => "Five",
                    "G" => "Six", 
                    "H" => "Seven", 
                    "I" => "Eight"
                );
printr($array['G' rr 'H']);
// PRINTS
// Array
// (
//     [0] => Six
//     [1] => Seven
// )

Extract a series of values out a dictionary:

my $array = array(  "A" => "Zero",
                    "B" => "One",
                    "C" => "Two",
                    "D" => "Three",
                    "E" => "Four",
                    "F" => "Five",
                    "G" => "Six", 
                    "H" => "Seven", 
                    "I" => "Eight"
                );
printr($array['G' rr 'H', 'F' rr 'D', 'B']);
// PRINTS
// Array
// (
//     [0] => Six
//     [1] => Seven
//     [2] => Five
//     [3] => Four
//     [4] => Three
//     [5] => One
// )

Normally splicing mode is only triggered when it finds an rr within the expression however you can trigger it manually by using the Tuple class like so:

my $array = array(  "Zero",
                    "One",
                    "Two",
                    "Three",
                    "Four",
                    "Five",
                    "Six", 
                    "Seven", 
                    "Eight"
                );
printr($array[new Tuple(1, 3, 5, 0)]);
// PRINTS
// Array
// (
//     [0] => One
//     [1] => Three
//     [2] => Five
//     [3] => Zero
// )

A shorter way to do the tuple splice is to use a Preprocessor macro like this:

// create the array
my $array = array(  "Zero",
                    "One",
                    "Two",
                    "Three",
                    "Four",
                    "Five",
                    "Six", 
                    "Seven", 
                    "Eight"
                );
// splice and print
printr($array[_T(1, 3, 5, 0)]);
// PRINTS
// Array
// (
//     [0] => One
//     [1] => Three
//     [2] => Five
//     [3] => Zero
// )

Tuples can also be multi-dimensional like so:

printr [[1,2,3],[4,5,6],[7,8,9]][_T([-1,0,1], [-1,0,1], [-1,0,1])];
// PRINTS
// Array
// (
//     [0] => Array
//         (
//             [0] => 3
//             [1] => 1
//             [2] => 2
//         )
//     [1] => Array
//         (
//             [0] => 3
//             [1] => 1
//             [2] => 2
//         )
//     [2] => Array
//         (
//             [0] => 3
//             [1] => 1
//             [2] => 2
//         )
// )

You can also store Tuples inside Tuples like so:

printr [[1,2,3],[4,5,6],[7,8,9]][_T(_T(-1,0,1), _T(-1,0,1), _T(-1,0,1))];
// PRINTS
// Array
// (
//     [0] => Array
//         (
//             [0] => 3
//             [1] => 1
//             [2] => 2
//         )
//     [1] => Array
//         (
//             [0] => 3
//             [1] => 1
//             [2] => 2
//         )
//     [2] => Array
//         (
//             [0] => 3
//             [1] => 1
//             [2] => 2
//         )
// )

Check out Slicing after this.


Contact
Cheryl (uberfox@hotmail.com)
Homepage
http://ubersoft.org