in reply to Array Slice Referencing
QUESTION 1: how could it be that array slice generates a scalar references when an array referencing generates an array reference?
See perlref:
Taking a reference to an enumerated list is not the same as using square brackets--instead it's the same as creating a list of references!@list = (\$a, \@b, \%c); @list = \($a, @b, %c); # same thing!
That is consistent with referencing an array slice, since slicing an array yields a list.
QUESTION 2: is there any other way to generate an array reference (not a scalar references) by picking a selected data to be referenced from an array? (what happens when @_ is replaced by a scalar variable, it only gets the first index from the array slice)
To generate a new array reference from an array slice, you would use the anonymous array constructor [] and place the array slice (which is a list) into that:
$list = [ @array[2,0] ];
If you assign an array slice to a scalar, you get the same behaviour as with assigning a list to a scalar.
$\ = "\n"; my @array = ("my","list","here"); $_ = @array[0,1,2]; print; # prints "here" - last element of slice $_ = ("my","list","here"); print; # also prints "here"
In scalar context, the comma operator just discards its left argument and returns the right argument (after evaluating it if it's a expression). So the expression in parens yields its last argument. See perlop. Again, an array slice is a list.
well for my second question the following might be a solution (inefficient enough):yes it is inefficient enough because it doesn't change the values of my orignal array, but only the value of my array reference. well that's very wierd because it only means that the reference consumes a new space in my memory in which it has its own data other than my array.#!perl/bin/perl use strict; my @array = ("my","list","here"); my @ref = ([@array[2,0]],[@array[1]]); $ref[0][0] = "changed"; print "$ref[0][0] @array";
If you want the elements of your newly created array of anonymous arrays to refer to the elements in your original array, you must take references to the elements of the original array instead of populating your newly created array references with copies of your original array elements:
my @array = ("my","list","here"); my @ref = ([\@array[2,0]],[\@array[1]]); ${$ref[0][0]} = "changed"; print "${$ref[0][0]} @array"; __END__ changed my list changed
QUESTION 3: how come it happens that creating a new reference also creates a space in my meomory based on that program?
That's because the following statements are equivalent:
my @ref = (\@array[2,0],\@array[1]); my @ref = \(@array[2,0],@array[1]); my @ref = (\$array[2],\$array[0],\$array[1]); my @ref = \($array[2],$array[0],$array[1]); my @ref = \(@array[2,0,1]);
You don't get two references, you get three references. See above.
update: minor wording change
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Array Slice Referencing
by PerlPhi (Sexton) on Jun 02, 2007 at 11:35 UTC |