Array Elements Assigned to a Slice : Slice « Array « Perl






Array Elements Assigned to a Slice

   


@digits = (11..21);
@slice[10..20] = (@digits);


print "@slice\n";
print "last index is $#slice.\n";

@09 = (0..9);
@slice[@09] = (@digits);
print "@slice\n";
print "last index is $#slice.\n";

@slice[1,3,5,7,9] = (2,4,6,8,10);
@evenNumbers = @slice[1,3,5,7,9];
print "array: @evenNumbers\n";
print "The last index is $#evenNumbers.\n";


@slice[@digits,77,55,33] = (1,2,3,4,4,5,6,6,7,5,6,7,6,7,8);
print "array are: @slice\n";
print "Indexes 55, 33, 77, and 12 in that order are: @slice[55,33,77,12]\n";
print "The last index of the array is $#slice.\n";
@names = (David, Mary, Thomas, Dewey, Steve, Martin);
printNames(@names);


for ($i=0; $i<=$#names; $i= $i+2){
   @names[$i+1, $i] = @names[$i, $i+1];
}
printNames(@names);

sub printNames (@){
   my (@names) = @_;
   for ($i=0; $i<= $#names;){
      print "$names[$i++], $names[$i++]\n";
   }
}

   
    
    
  








Related examples in the same category

1.Array Slices
2.Array slices 2
3.A program that assigns to an array slice.
4.A program containing overlapping array slices.
5.A program that uses an array variable as an array-slice subscript.
6.A program that demonstrates the use of an array slice.
7.Displaying slices of @array
8.Slice an array by referencing the index in a sub range
9.Slicing out part of an array to make a sort of subarray