tomy $ref = \{@list[2..4]};
and you should have what you want.my $ref = [@list[2..4]]
(update: well not a reference but a copy, as my fellow monks have kindly pointed out, and sauoq (among others) explains nicely below).
What $ref is holding is a reference to a reference of an anonymous hash. (what happens with the curly braces, rather than with the square ones.) As Data::Dumper clearly shows:
#!/usr/bin/perl use Data::Dumper; use strict; use warnings; my @list = (0, 1, 2, 3, 4, 5, 6, 7); print "@list\n"; my @short = @list[1..4]; print "@short\n"; my $ref = \{@list[2..4]}; print Dumper $ref; __DATA__ 0 1 2 3 4 5 6 7 1 2 3 4 Odd number of elements in anonymous hash at C:\test_bed\testme.pl line + 10. $VAR1 = \{ '4' => undef, '2' => 3 };
-enlil
In reply to Re: reference to an array slice?
by Enlil
in thread reference to an array slice?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |