PerlPhi has asked for the wisdom of the Perl Monks concerning the following question:
hi,,, when i was trying to use an array slice and referenced it to an array, a scalar references was generated. codes using array slice referencing as follows:
#!perl/bin/perl use strict; my @array = ("my","list","here"); @_ = \@array[2,0]; print @_;
the array slice generates a scalar references and prints out those two references.
QUESTION 1: how could it be that array slice generates a scalar references when an array referencing generates an array reference?
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)
well for my second question the following might be a solution (inefficient enough):
#!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";
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.
QUESTION 3: how come it happens that creating a new reference also consumes a space in my meomory based on that program?
the following codes may prevent that to happen but my earlier delimma (about array slice referencing generates a scalar reference rather than an array reference) well show up again:
#!perl/bin/perl use strict; my @array = ("my","list","here"); my @ref = (\@array[2,0],\@array[1]); ${$ref[0]} = "changed"; print "${$ref[0]} @array";
now the values of my array changed as my reference is given a new value but then my earlier delimma still exist. its really weary when one problem got passed and the other one prevails... maybe its beacuse of using anonymous array (autovivification) consumes a new space in my memory other than my array variable...
thanks in advance... keep deep and dark!
From: PerlPhi
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Array Slice Referencing
by shmem (Chancellor) on Jun 02, 2007 at 10:41 UTC | |
by PerlPhi (Sexton) on Jun 02, 2007 at 11:35 UTC | |
|
Re: Array Slice Referencing
by FunkyMonk (Bishop) on Jun 02, 2007 at 10:36 UTC | |
|
Re: Array Slice Referencing
by BrowserUk (Patriarch) on Jun 02, 2007 at 17:50 UTC | |
by TimToady (Parson) on Jun 02, 2007 at 20:42 UTC | |
by Jenda (Abbot) on Jun 02, 2007 at 21:52 UTC | |
by BrowserUk (Patriarch) on Jun 02, 2007 at 23:19 UTC |