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):
#!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.

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}

In reply to Re: Array Slice Referencing by shmem
in thread Array Slice Referencing by PerlPhi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.