The first argument to shift
and pop
must be an array (@NAME
or @BLOCK
), not a map
or grep
operator (which have nothing to do with arrays).
You could build an array from which to shift
/pop
.
my $first_ele = shift @{ [ map { 10 * $_ } @arr ] }; # If you want first
my $last_ele = pop @{ [ map { 10 * $_ } @arr ] }; # If you want last
But that's very wasteful. You could avoid the array creation by using a list assignment or a list slice.
my ($first_ele) = map { 10 * $_ } @arr; # If you want first
my $first_ele = ( map { 10 * $_ } @arr )[0]; # If you want first
my $last_ele = ( map { 10 * $_ } @arr )[-1]; # If you want last
my ($first_ele, $last_ele) = ( map { 10 * $_ } @arr )[0, -1]; # If you want both
But that's still wasteful. There's no need to multiply all the elements when you only want the product of last one. The following makes far more sense:
my $first_ele = 10 * $arr[0]; # If you want first
my $last_ele = 10 * $arr[-1]; # If you want last
my ($first_ele, $last_ele) = map { 10 * $_ } @arr[0, -1]; # If you want both