a reference to an element of an array or hash, how to create?

Discussion in 'Perl Misc' started by S P Arif Sahari Wibowo, Aug 5, 2008.

  1. Hi!

    Do you know how to create a reference to an element of an array
    or hash? For instance I have an array @a, and I like $a2r
    contains a reference to $a[2]. Anyway to do it in perl?

    Keep in mind this is not the case where $a[2] contain a
    reference and $a2r contains the same reference, but instead $a2r
    contain a reference to the scalar stored in $a[2].

    Let me explain more using the pseudo code below, RefTo represent
    the mechanism in question:

    my @a= (1, 2, 3, 4);

    my $p= RefTo $a[2];

    $$p= 9;

    print join(",", @a) # should print 1,2,9,4


    So, any idea wether there is such mechanism as RefTo in Perl?

    Thanks!

    --
    (stephan paul) Arif Sahari Wibowo
    _____ _____ _____ _____
    /____ /____/ /____/ /____
    _____/ / / / _____/ http://www.arifsaha.com/
    S P Arif Sahari Wibowo, Aug 5, 2008
    #1
    1. Advertising

  2. S P Arif Sahari Wibowo

    Guest

    S P Arif Sahari Wibowo <> wrote:
    > Hi!
    >
    > Do you know how to create a reference to an element of an array
    > or hash? For instance I have an array @a, and I like $a2r
    > contains a reference to $a[2]. Anyway to do it in perl?


    The Perl "reference to" operator is the backslash.

    >
    > my @a= (1, 2, 3, 4);
    >
    > my $p= RefTo $a[2];
    >
    > $$p= 9;
    >
    > print join(",", @a) # should print 1,2,9,4


    perl -le 'my @x=(1..4); my $p=\$x[2]; $$p=9; print "@x"'
    1 2 9 4

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
    , Aug 5, 2008
    #2
    1. Advertising

  3. S P Arif Sahari Wibowo wrote:
    >
    > Do you know how to create a reference to an element of an array or hash?
    > For instance I have an array @a, and I like $a2r contains a reference to
    > $a[2]. Anyway to do it in perl?
    >
    > Keep in mind this is not the case where $a[2] contain a reference and
    > $a2r contains the same reference, but instead $a2r contain a reference
    > to the scalar stored in $a[2].
    >
    > Let me explain more using the pseudo code below, RefTo represent the
    > mechanism in question:
    >
    > my @a= (1, 2, 3, 4);
    >
    > my $p= RefTo $a[2];
    >
    > $$p= 9;
    >
    > print join(",", @a) # should print 1,2,9,4


    $ perl -le'my @a = ( 1, 2, 3, 4 ); my $p = \$a[ 2 ]; $$p = 9; print join
    ",", @a'
    1,2,9,4



    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
    John W. Krahn, Aug 5, 2008
    #3
  4. S P Arif Sahari Wibowo

    Zak B. Elep Guest

    S P Arif Sahari Wibowo <> writes:

    > Do you know how to create a reference to an element of an array or hash?
    > For instance I have an array @a, and I like $a2r contains a reference to
    > $a[2]. Anyway to do it in perl?


    ,----[ perl -de 0 ]
    | alteran:~ zakame$ perl -de 0
    |
    | Loading DB routines from perl5db.pl version 1.28
    | Editor support available.
    |
    | Enter h or `h h' for help, or `man perldebug' for more help.
    |
    | main::(-e:1): 0
    | DB<1> @a = ( 1, 2, 3 )
    |
    | DB<2> $a2r = \$a[2]
    |
    | DB<3> x $a2r
    | 0 SCALAR(0x190fe24)
    | -> 3
    | DB<4> x $$a2r
    | 0 3
    `----


    --
    I like the idea of 256 bits, though: 32 for the (Unicode) character leaves
    room for 224 Bucky bits, which ought to be enough for anyone.
    -- Roland Hutchinson, in alt.folklore.computers
    Zak B. Elep, Aug 5, 2008
    #4
  5. On Tue, 5 Aug 2008, wrote:
    > perl -le 'my @x=(1..4); my $p=\$x[2]; $$p=9; print "@x"'


    On Tue, 5 Aug 2008, John W. Krahn wrote:
    > $ perl -le'my @a = ( 1, 2, 3, 4 ); my $p = \$a[ 2 ]; $$p = 9; print join ",", @a'


    Duh! I should try it first, didn't think it will be that easy
    before.

    Thanks everyone!

    --
    (stephan paul) Arif Sahari Wibowo
    _____ _____ _____ _____
    /____ /____/ /____/ /____
    _____/ / / / _____/ http://www.arifsaha.com/
    S P Arif Sahari Wibowo, Aug 5, 2008
    #5
    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. rp
    Replies:
    1
    Views:
    491
    red floyd
    Nov 10, 2011
  2. Anthony Martinez
    Replies:
    4
    Views:
    258
    Robert Klemme
    Jun 11, 2007
  3. Michal Suchanek
    Replies:
    6
    Views:
    214
    Nobuyoshi Nakada
    Jun 13, 2007
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    595
    David A. Black
    Jul 2, 2008
  5. Iulian Ilea

    problem: create element in create element

    Iulian Ilea, Dec 14, 2006, in forum: Javascript
    Replies:
    4
    Views:
    175
    Iulian Ilea
    Dec 17, 2006
Loading...

Share This Page