views:

86

answers:

3

I have a reference to an array $arr_ref. I would like to get a reference to an array containing only cells i..j in the original array.

+5  A: 
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];
FM
+1 Thanks for the info
David B
+4  A: 
@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
codaddict
+7  A: 
@slice = @{$arr_ref}[$i..$j];
Alex Reynolds