| 37 | | my @array3 = (@array1, @array2); |
| 38 | | isa_ok(@array3, 'Array'); |
| 39 | | |
| 40 | | is(+@array3, 6, 'the array3 has 6 elements'); |
| 41 | | is(@array3[0], 'foo', 'got the right value at array3 index 0'); |
| 42 | | is(@array3[1], 'bar', 'got the right value at array3 index 1'); |
| 43 | | is(@array3[2], 'baz', 'got the right value at array3 index 2'); |
| 44 | | is(@array3[3], 'test', 'got the right value at array3 index 3'); |
| 45 | | is(@array3[4], 1, 'got the right value at array3 index 4'); |
| 46 | | is(@array3[5], undef, 'got the right value at array3 index 5'); |
| 47 | | |
| 48 | | |
| 49 | | # array slice |
| 50 | | my @array4 = @array2[2, 1, 0]; |
| 51 | | isa_ok(@array4, 'Array'); |
| 52 | | |
| 53 | | is(+@array4, 3, 'the array4 has 3 elements'); |
| 54 | | is(@array4[0], undef, 'got the right value at array4 index 0'); |
| 55 | | is(@array4[1], 1, 'got the right value at array4 index 1'); |
| 56 | | is(@array4[2], 'test', 'got the right value at array4 index 2'); |
| 57 | | |
| 58 | | # create new array with 2 array slices |
| 59 | | my @array5 = ( @array2[2, 1, 0], @array1[2, 1, 0] ); |
| 60 | | isa_ok(@array5, 'Array'); |
| 61 | | |
| 62 | | is(+@array5, 6, 'the array5 has 6 elements'); |
| 63 | | is(@array5[0], undef, 'got the right value at array5 index 0'); |
| 64 | | is(@array5[1], 1, 'got the right value at array5 index 1'); |
| 65 | | is(@array5[2], 'test', 'got the right value at array5 index 2'); |
| 66 | | is(@array5[3], 'baz', 'got the right value at array5 index 3'); |
| 67 | | is(@array5[4], 'bar', 'got the right value at array5 index 4'); |
| 68 | | is(@array5[5], 'foo', 'got the right value at array5 index 5'); |
| 69 | | |
| 70 | | # create an array slice with an array (in a variable) |
| 71 | | |
| 72 | | my @slice = (2, 0, 1); |
| 73 | | my @array6 = @array1[@slice]; |
| 74 | | isa_ok(@array6, 'Array'); |
| 75 | | |
| 76 | | is(+@array6, 3, 'the array6 has 3 elements'); |
| 77 | | is(@array6[0], 'baz', 'got the right value at array6 index 0'); |
| 78 | | is(@array6[1], 'foo', 'got the right value at array6 index 1'); |
| 79 | | is(@array6[2], 'bar', 'got the right value at array6 index 2'); |
| 80 | | |
| 81 | | # create an array slice with an array constructed with () |
| 82 | | |
| 83 | | my @array7 = @array1[(2, 1, 0)]; |
| 84 | | isa_ok(@array7, 'Array'); |
| 85 | | |
| 86 | | is(+@array7, 3, 'the array7 has 3 elements'); |
| 87 | | is(@array7[0], 'baz', 'got the right value at array7 index 0'); |
| 88 | | is(@array7[1], 'bar', 'got the right value at array7 index 1'); |
| 89 | | is(@array7[2], 'foo', 'got the right value at array7 index 2'); |
| 90 | | |
| 91 | | # odd slices |
| 92 | | |
| 93 | | my $result1 = (1, 2, 3, 4)[1]; |
| 94 | | is($result1, 2, 'got the right value from the slice'); |
| 95 | | |
| 96 | | my $result2 = [1, 2, 3, 4][2]; |
| 97 | | is($result2, 3, 'got the right value from the slice'); |
| | 36 | { |
| | 37 | my @array3 = (@array1, @array2); |
| | 38 | isa_ok(@array3, 'Array'); |
| | 39 | |
| | 40 | is(+@array3, 6, 'the array3 has 6 elements'); |
| | 41 | is(@array3[0], 'foo', 'got the right value at array3 index 0'); |
| | 42 | is(@array3[1], 'bar', 'got the right value at array3 index 1'); |
| | 43 | is(@array3[2], 'baz', 'got the right value at array3 index 2'); |
| | 44 | is(@array3[3], 'test', 'got the right value at array3 index 3'); |
| | 45 | is(@array3[4], 1, 'got the right value at array3 index 4'); |
| | 46 | is(@array3[5], undef, 'got the right value at array3 index 5'); |
| | 47 | } |
| | 48 | |
| | 49 | { |
| | 50 | # array slice |
| | 51 | my @array4 = @array2[2, 1, 0]; |
| | 52 | isa_ok(@array4, 'Array'); |
| | 53 | |
| | 54 | is(+@array4, 3, 'the array4 has 3 elements'); |
| | 55 | is(@array4[0], undef, 'got the right value at array4 index 0'); |
| | 56 | is(@array4[1], 1, 'got the right value at array4 index 1'); |
| | 57 | is(@array4[2], 'test', 'got the right value at array4 index 2'); |
| | 58 | } |
| | 59 | |
| | 60 | { |
| | 61 | # create new array with 2 array slices |
| | 62 | my @array5 = ( @array2[2, 1, 0], @array1[2, 1, 0] ); |
| | 63 | isa_ok(@array5, 'Array'); |
| | 64 | |
| | 65 | is(+@array5, 6, 'the array5 has 6 elements'); |
| | 66 | is(@array5[0], undef, 'got the right value at array5 index 0'); |
| | 67 | is(@array5[1], 1, 'got the right value at array5 index 1'); |
| | 68 | is(@array5[2], 'test', 'got the right value at array5 index 2'); |
| | 69 | is(@array5[3], 'baz', 'got the right value at array5 index 3'); |
| | 70 | is(@array5[4], 'bar', 'got the right value at array5 index 4'); |
| | 71 | is(@array5[5], 'foo', 'got the right value at array5 index 5'); |
| | 72 | } |
| | 73 | |
| | 74 | { |
| | 75 | # create an array slice with an array (in a variable) |
| | 76 | |
| | 77 | my @slice = (2, 0, 1); |
| | 78 | my @array6 = @array1[@slice]; |
| | 79 | isa_ok(@array6, 'Array'); |
| | 80 | |
| | 81 | is(+@array6, 3, 'the array6 has 3 elements'); |
| | 82 | is(@array6[0], 'baz', 'got the right value at array6 index 0'); |
| | 83 | is(@array6[1], 'foo', 'got the right value at array6 index 1'); |
| | 84 | is(@array6[2], 'bar', 'got the right value at array6 index 2'); |
| | 85 | } |
| | 86 | |
| | 87 | { |
| | 88 | # create an array slice with an array constructed with () |
| | 89 | my @array7 = @array1[(2, 1, 0)]; |
| | 90 | isa_ok(@array7, 'Array'); |
| | 91 | |
| | 92 | is(+@array7, 3, 'the array7 has 3 elements'); |
| | 93 | is(@array7[0], 'baz', 'got the right value at array7 index 0'); |
| | 94 | is(@array7[1], 'bar', 'got the right value at array7 index 1'); |
| | 95 | is(@array7[2], 'foo', 'got the right value at array7 index 2'); |
| | 96 | } |
| | 97 | |
| | 98 | { |
| | 99 | # odd slices |
| | 100 | my $result1 = (1, 2, 3, 4)[1]; |
| | 101 | is($result1, 2, 'got the right value from the slice'); |
| | 102 | |
| | 103 | my $result2 = [1, 2, 3, 4][2]; |
| | 104 | is($result2, 3, 'got the right value from the slice'); |
| | 105 | } |
| 133 | | } |
| 134 | | |
| 135 | | # end index range |
| 136 | | is ~@array12[*-4 .. *-2], 'a b c', "end indices [*-4 .. *-2]"; |
| 137 | | |
| 138 | | # end index as lvalue |
| 139 | | @array12[*-1] = 'd'; |
| 140 | | is @array12[*-1], 'd', "assigns to the correct end slice index"; |
| 141 | | is ~@array12,'a b c d', "assignment to end index correctly alters the array"; |
| 142 | | |
| 143 | | my @array13 = ('a', 'b', 'c', 'd'); |
| 144 | | # end index range as lvalue |
| 145 | | @array13[*-4 .. *-1] = ('d', 'c', 'b', 'a'); # ('a'..'d').reverse |
| 146 | | is ~@array13, 'd c b a', "end range as lvalue"; |
| 147 | | |
| 148 | | #hat trick |
| 149 | | my @array14 = ('a', 'b', 'c', 'd'); |
| 150 | | my @b = 0..3; |
| 151 | | ((@b[*-3,*-2,*-1,*-4] = @array14)= @array14[*-1,*-2,*-3,*-4]); |
| 152 | | |
| 153 | | is ~@b, |
| 154 | | 'a d c b', |
| 155 | | "hat trick: |
| 156 | | assign to a end-indexed slice array from array |
| 157 | | lvalue in assignment is then lvalue to end-indexed slice as rvalue"; |
| | 144 | |
| | 145 | # end index range |
| | 146 | is ~@array12[*-4 .. *-2], 'a b c', "end indices [*-4 .. *-2]"; |
| | 147 | |
| | 148 | # end index as lvalue |
| | 149 | @array12[*-1] = 'd'; |
| | 150 | is @array12[*-1], 'd', "assigns to the correct end slice index"; |
| | 151 | is ~@array12,'a b c d', "assignment to end index correctly alters the array"; |
| | 152 | } |
| | 153 | |
| | 154 | { |
| | 155 | my @array13 = ('a', 'b', 'c', 'd'); |
| | 156 | # end index range as lvalue |
| | 157 | @array13[*-4 .. *-1] = ('d', 'c', 'b', 'a'); # ('a'..'d').reverse |
| | 158 | is ~@array13, 'd c b a', "end range as lvalue"; |
| | 159 | |
| | 160 | #hat trick |
| | 161 | my @array14 = ('a', 'b', 'c', 'd'); |
| | 162 | my @b = 0..3; |
| | 163 | ((@b[*-3,*-2,*-1,*-4] = @array14)= @array14[*-1,*-2,*-3,*-4]); |
| | 164 | |
| | 165 | is ~@b, |
| | 166 | 'a d c b', |
| | 167 | "hat trick: |
| | 168 | assign to a end-indexed slice array from array |
| | 169 | lvalue in assignment is then lvalue to end-indexed slice as rvalue"; |
| | 170 | } |