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

Home
Perl
1.Array
2.CGI
3.Class
4.Data Type
5.Database
6.File
7.GUI
8.Hash
9.Language Basics
10.Network
11.Regular Expression
12.Report
13.Statement
14.String
15.Subroutine
16.System Functions
17.Win32
18.XML
Perl » Array » Slice 
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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.