Modifiying array elements with references

Discussion in 'Perl Misc' started by Anonymous user, Jan 25, 2006.

  1. Hello,

    i'm a complete newB in perl especialy for references. I would like to
    be able to modify some array elements through reference, because i
    don't want a new array.

    I would like to make the following script work:

    ------------------------------------------------------>8
    #!/usr/bin/perl

    use strict;
    use warnings;

    my @array = ( "1", "2", "3" ); # array of elements

    print("before, element 1 = $array[1]\n");

    my @refs = \(@array); # array of element references
    my $elt_1 = $refs[1]; # reference on 2nd array element

    print("before, element 1 = $elt_1\n"); # should print "2"

    $elt_1 = "changed"; # should modify the array

    print("after, element 1 = $array[1]\n"); # should print "changed"
    ------------------------------------------------------>8

    by should i mean "i'd like to" ;)

    TIA for your help
     
    Anonymous user, Jan 25, 2006
    #1
    1. Advertising

  2. Anonymous user wrote:
    > I would like to
    > be able to modify some array elements through reference,
    >
    > I would like to make the following script work:
    >
    > ------------------------------------------------------>8
    > #!/usr/bin/perl
    >
    > use strict;
    > use warnings;
    >
    > my @array = ( "1", "2", "3" ); # array of elements
    >
    > print("before, element 1 = $array[1]\n");
    >
    > my @refs = \(@array); # array of element references
    > my $elt_1 = $refs[1]; # reference on 2nd array element
    >
    > print("before, element 1 = $elt_1\n"); # should print "2"


    In that case you need to dereference $elt_1:

    print "before, element 1 = $$elt_1\n";
    -------------------------------^^

    > $elt_1 = "changed"; # should modify the array


    You probably meant:

    $$elt_1 = "changed";
    ----^^

    > print("after, element 1 = $array[1]\n"); # should print "changed"


    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jan 25, 2006
    #2
    1. Advertising

  3. thx a lot !!
     
    Anonymous user, Jan 25, 2006
    #3
  4. Anonymous user

    Anno Siegel Guest

    Anonymous user <> wrote in comp.lang.perl.misc:
    > Hello,
    >
    > i'm a complete newB in perl especialy for references. I would like to
    > be able to modify some array elements through reference, because i
    > don't want a new array.
    >
    > I would like to make the following script work:
    >
    > ------------------------------------------------------>8
    > #!/usr/bin/perl
    >
    > use strict;
    > use warnings;
    >
    > my @array = ( "1", "2", "3" ); # array of elements
    >
    > print("before, element 1 = $array[1]\n");
    >
    > my @refs = \(@array); # array of element references
    > my $elt_1 = $refs[1]; # reference on 2nd array element
    >
    > print("before, element 1 = $elt_1\n"); # should print "2"
    >
    > $elt_1 = "changed"; # should modify the array
    >
    > print("after, element 1 = $array[1]\n"); # should print "changed"
    > ------------------------------------------------------>8


    Gunnar has pointed out your failure to de-reference the scalar
    refs you are using. Are you aware that you could also modify the
    array through an array ref? That would only be one ref, instead
    of one for each element that needs to be changed.

    Both methods have applications, but the use of an array ref is
    preferred, if possible.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Jan 25, 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. Roger Leigh
    Replies:
    8
    Views:
    468
    Karl Heinz Buchegger
    Nov 17, 2003
  2. P
    Replies:
    1
    Views:
    1,191
    Joe Kesselman
    Jul 7, 2006
  3. brf
    Replies:
    2
    Views:
    919
    Knute Johnson
    Dec 4, 2006
  4. Mike Collier
    Replies:
    2
    Views:
    133
    Mike Collier
    Jan 20, 2004
  5. fatted
    Replies:
    1
    Views:
    129
    Fatted
    Jul 24, 2003
Loading...

Share This Page