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


In reply to 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.