in reply to reference to an array slice?

Change this line:
my $ref = \{@list[2..4]};
to
my $ref = [@list[2..4]]
and you should have what you want.

(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

Replies are listed 'Best First'.
Re: Re: reference to an array slice?
by sauoq (Abbot) on Sep 25, 2003 at 21:53 UTC

    As Limbic~Region points out in his reply, this isn't the same thing as a "reference to a slice". It is functionally equivalent to making a copy of the slice and using a reference to that copy.

    -sauoq
    "My two cents aren't worth a dime.";