Reputation: 451
Consider the following lines of code. I want to slice the array ref $a and return the result as an array ref assigned to $b. I can do that in two lines as shown. I am stumped in my attempts to do this in one line! How can this be done?
$a = [1,2,3,4,5];
###the desired result###########################
@b = @{$a}[1 .. @{$a} - 1];
$b = \@b; # $b is [2,3,4,5]
################################################
###trying to get the desired result in one line##
$b = \@{$a}[1 .. @{$a} - 1]; # $b is \5;
$b = \{@{$a}[1 .. @{$a} - 1]}; # $b is \{ 2 => 3, 4 => 5 }
$b = $a->[1 .. @{$a} - 1]; # $b is 1
$b = $a->@[1 .. @{$a} - 1]; # $b is 5
Upvotes: 1
Views: 86