开发者

How do I get a slice of a referenced array in Perl?

I have a reference to an array $arr_ref. I would like to get a reference to开发者_如何学C an array containing only cells i..j in the original array.


@slice = @{$arr_ref}[$i..$j];


my $r = [0..9];    
print $_, "\n" for @$r[3..5];

If the variable containing the reference is more complex than an ordinary scalar, enclose it in braces. This is needed because dereferencing happens before subscript lookup:

my @refs = ( [0..9], [100..109] );
print $_, "\n" for @{ $refs[1] }[4..8];


@rainbow = ("red", "green", "blue", "yellow", "orange", "violet", "indigo");
$arr_ref = \@rainbow;
$i = 1;
$j = 3;
@slice = @$arr_ref[$i..$j]; # @slice is now green blue yellow
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜