light weight references and arrays

Discussion in 'Perl Misc' started by xhoster@gmail.com, Aug 3, 2006.

  1. Guest

    Arrays hold some kind of special light-weight reference to it's scalar
    members, but of course the reference it holds are not held in full scalars,
    as that would cause an infinite regress. Is there some way to manipulate
    these light-weight references directly?


    The code below makes a copy of all the data in $y:

    $ perl -le 'my @x=1..10; my $y = "x"x1e7; $x[0]=$y'

    This codes doesn't make a copy, but now I need to manually derefence
    it when I want to retrieve it from @x:

    $ perl -le 'my @x=1..10; my $y = "x"x1e7; $x[0]=\$y'

    And this code just doens't work:

    $ perl -le 'my @x=1..10; my $y = "x"x1e7; \$x[0]=\$y'
    Can't modify single ref constructor in scalar assignment at -e line 1, at EOF

    I think the above code makes it clear what I intended to do, change the SV
    that a certain array slot is referencing to be a different SV. Is there a
    fundamental reason that Perl cannot be made to this, or is it simply that
    it hasn't been coded that way?

    A similar question goes for concatenating arrays--is there a way to
    accomplish something like push @x,@y but having the scalars contained in @y
    be directly incorporated into the end of @x, rather than being copied into
    new scalars that are on the end of @x? (unlike the original, I don't know
    what syntax would be used to imply this operation)


    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Aug 3, 2006
    #1
    1. Advertising

  2. Uri Guttman Guest

    >>>>> "x" == xhoster <> writes:

    x> I think the above code makes it clear what I intended to do, change
    x> the SV that a certain array slot is referencing to be a different
    x> SV. Is there a fundamental reason that Perl cannot be made to
    x> this, or is it simply that it hasn't been coded that way?

    i think local can do that as it will work on any element in an array or
    hash. should be easy to test for your needs. perl6 can do that almost
    surely as it has a bind := op.

    x> A similar question goes for concatenating arrays--is there a way to
    x> accomplish something like push @x,@y but having the scalars contained in @y
    x> be directly incorporated into the end of @x, rather than being copied into
    x> new scalars that are on the end of @x? (unlike the original, I don't know
    x> what syntax would be used to imply this operation)

    what you want is lazy evaluation which perl6 will also have. you can
    push a whole array and only when you access that section it will it be
    accessed and then via the original array (if it hasn't been modified,
    copy on write rules are in effect).

    of course i could be wrong this week as perl6 changes faster then a
    submachine gun based tv remote control!

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Aug 3, 2006
    #2
    1. Advertising

  3. Dr.Ruud Guest

    schreef:

    > Arrays hold some kind of special light-weight reference to it's scalar
    > members, but of course the reference it holds are not held in full
    > scalars, as that would cause an infinite regress. Is there some way
    > to manipulate these light-weight references directly?
    >
    >
    > The code below makes a copy of all the data in $y:
    >
    > $ perl -le 'my @x=1..10; my $y = "x"x1e7; $x[0]=$y'
    >
    > This codes doesn't make a copy, but now I need to manually derefence
    > it when I want to retrieve it from @x:
    >
    > $ perl -le 'my @x=1..10; my $y = "x"x1e7; $x[0]=\$y'
    >
    > And this code just doens't work:
    >
    > $ perl -le 'my @x=1..10; my $y = "x"x1e7; \$x[0]=\$y'
    > Can't modify single ref constructor in scalar assignment at -e line
    > 1, at EOF
    >
    > I think the above code makes it clear what I intended to do, change
    > the SV that a certain array slot is referencing to be a different SV.
    > Is there a fundamental reason that Perl cannot be made to this, or is
    > it simply that
    > it hasn't been coded that way?
    >
    > A similar question goes for concatenating arrays--is there a way to
    > accomplish something like push @x,@y but having the scalars contained
    > in @y be directly incorporated into the end of @x, rather than being
    > copied into new scalars that are on the end of @x? (unlike the
    > original, I don't know what syntax would be used to imply this
    > operation)


    I think you are looking for
    http://search.cpan.org/search?module=Data::Alias

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Aug 4, 2006
    #3
  4. Guest

    "Dr.Ruud" <> wrote:
    > schreef:
    >
    > > Arrays hold some kind of special light-weight reference to it's scalar
    > > members, but of course the reference it holds are not held in full
    > > scalars, as that would cause an infinite regress. Is there some way
    > > to manipulate these light-weight references directly?

    ....
    > >
    > > A similar question goes for concatenating arrays--is there a way to
    > > accomplish something like push @x,@y but having the scalars contained
    > > in @y be directly incorporated into the end of @x, rather than being
    > > copied into new scalars that are on the end of @x? (unlike the
    > > original, I don't know what syntax would be used to imply this
    > > operation)

    >
    > I think you are looking for
    > http://search.cpan.org/search?module=Data::Alias


    Thanks. That looks very nice, and just what I wanted.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Aug 4, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. moondaddy
    Replies:
    4
    Views:
    755
    S. Justin Gengo
    Jun 29, 2004
  2. Song Ching Koh
    Replies:
    2
    Views:
    9,951
    Nils O. =?iso-8859-1?Q?Sel=E5sdal?=
    Oct 31, 2003
  3. j
    Replies:
    2
    Views:
    391
    James Birchfield
    Nov 26, 2003
  4. Malcolm Greene
    Replies:
    7
    Views:
    634
    Stefan Behnel
    Mar 7, 2008
  5. Nasir_Jointecn

    Light weight laptop

    Nasir_Jointecn, Aug 6, 2008, in forum: HTML
    Replies:
    1
    Views:
    395
    richard
    Aug 6, 2008
Loading...

Share This Page