3.1.8 Assigning Values to a Slice of an Array
It is permissible to perform an assignment to a slice of an array. For example, we can rewrite the previous program as follows.
Program 3.3
#!/usr/bin/perl
#file friends4.pl
use strict;
my (@friends, $i);
@friends = ('John Hart', 'Jeff Ellis', 'Brett Walters',
'Blake Escort', 'Tom Toybee');
$friends [20] = 'Jeff Perich';
#Setting a slice
@friends [5..7] = ("Ramen Talukdar", "Sekhar Ojha", "Amar Kalita");
for ($i=0; $i <= $#friends; $i++){
if (defined $friends[$i]){
print "$i\t", lc ($friends[$i]), "\n";
}
}
Here @friends[5..7] gives values to previously undefined values. The output of this program is given below.
0 john hart 1 jeff ellis 2 brett walters 3 blake escort 4 tom toybee 5 ramen talukdar 6 sekhar ojha 7 amar kalita 20 jeff perich
If the slice @friends[5..7] had values from before, the values will be overwritten.
