http://www.perlmonks.org?node_id=1002112


in reply to @array elements multiplication with another array elements.

Point to note, the for loop uses the construct of scalar(@arr) and the map operation uses the special variable $# (+ arrayname) $#arr to determine the length of the list being implemented within the operations.

where @arr = (1,2,3,4)

scalar(@arr) returns the number of items in the list scalar(@arr) = 4

$#arr returns a zero indexed length of items list $#arr = 3

$#arr returns the highest index of the array $#arr = 3

The C style for loop has the condition of the increment being lower than scalar(@arr) where;

scalar(@arr) = 4, ( $i = 0, $i < scalar(@arr), $i++ ) = 0,1,2,3

The perl map operation however, inserts the incremented counter for the $_ variable where;

$#arr = 3, 0..$#arr = 0,1,2,3

updates to definition of $# variable as result of reply from Anomolous.

Should the $ARRAY_BASE special variable $[ be set to other than the default of 0. The $#arr may return an offset to the zero indexed result that you are probably not expecting. Generally use of this variable is frowned upon at best. However.. dah dih dah dih.


my @arr = split //,'Coyote'; print map {$arr[$_]} 0..$#arr;