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.
views:
86answers:
3
+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
2010-09-14 12:35:49
+1 Thanks for the info
David B
2010-09-14 14:48:20
+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
2010-09-14 12:36:58