Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Dear monks, I'd like to have a reference to an array element (or hash element) which is updated as the original array is changed. See code below:
print "--------Trying with arrays-------\n"; my @a = (0, 1, 2, 3); my $ar = \@a; # Array reference my $air = \$a[0]; # Reference to member of an array print "Define array >@a<\nCreate a reference to \$a[0]\n"; print "array ref: >@$ar< \n"; print "array index ref: >$$air<\n"; @a = ('a','b','c'); print "Change array to >@a<\n"; print "array ref: >@$ar< \n"; print "array index ref: >$$air<\n"; # <--- I'd like to get "a" here print "--------Trying with strings-------\n"; my $s = "string"; my $sr = \$s; print "orignal: >$s<\n"; print "reference: >$$sr<\n"; $s = "changed string"; print "string: >$s<\n"; print "string reference: >$$sr<\n";
--------Trying with arrays------- Define array >0 1 2 3< Create a reference to $a[0] array ref: >0 1 2 3< array index ref: >0< Change array to >a b c< array ref: >a b c< array index ref: >0< --------Trying with strings------- orignal: >string< reference: >string< string: >changed string< string reference: >changed string<

It works with the string, but it doesn't work with the array element. I'd like to get the updated array element when dereferencing. This example is simplified. The original problem is a referenced variable for a Tk Checkbutton where the referent get's updated by another subroutine, but the reference still shows the old value. I don't want to update the data through the reference as it's a more complex structure which makes it much easier to asign a whole new array instead of looping through it and update each single element.

Help, ideas, .. are greatly appreciated as I couldn't find anything suitable in the net or I'm looking for the wrong phrases. ;

In reply to Update references by Anonymous Monk

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (8)
    As of 2018-03-17 15:36 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (224 votes). Check out past polls.