Tuesday, 11 March 2014

Slicing Array Elements



Slicing Array Elements

You can also extract a "slice" from an array - that is, you can select more than one item from an array in order to produce another array.
#! /usr/bin/perl
@days = qw/Mon Tue Wed Thu Fri Sat Sun/;
@weekdays = @days[3,4,5];
print "@weekdays\n";
This will produce following result:
Thu Fri Sat
Note:-The specification for a slice must a list of valid indices, either positive or negative, each separated by a comma. For speed, you can also use the .. range operator:
#! /usr/bin/perl
 
@days = qw/Mon Tue Wed Thu Fri Sat Sun/;
 
@weekdays = @days[3..5];
 
print "@weekdays\n";
This will produce following result:
Thu Fri Sat

No comments: