<?xml version="1.0" encoding="windows-1252"?>
<node id="970288" title="Update references" created="2012-05-13 10:30:58" updated="2012-05-13 10:30:58">
<type id="115">
perlquestion</type>
<author id="961">
Anonymous Monk</author>
<data>
<field name="doctext">
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:

&lt;code&gt;
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 &gt;@a&lt;\nCreate a reference to \$a[0]\n";
print "array ref: &gt;@$ar&lt; \n";
print "array index ref: &gt;$$air&lt;\n";

@a = ('a','b','c');
print "Change array to &gt;@a&lt;\n";
print "array ref: &gt;@$ar&lt; \n";
print "array index ref: &gt;$$air&lt;\n";   # &lt;--- I'd like to get "a" here

print "--------Trying with strings-------\n";
my $s = "string";
my $sr = \$s;

print "orignal: &gt;$s&lt;\n";
print "reference: &gt;$$sr&lt;\n";

$s = "changed string";

print "string: &gt;$s&lt;\n";
print "string reference: &gt;$$sr&lt;\n";

&lt;/code&gt;
OUPUT:
&lt;code&gt;
--------Trying with arrays-------
Define array &gt;0 1 2 3&lt;
Create a reference to $a[0]
array ref: &gt;0 1 2 3&lt; 
array index ref: &gt;0&lt;
Change array to &gt;a b c&lt;
array ref: &gt;a b c&lt; 
array index ref: &gt;0&lt;
--------Trying with strings-------
orignal: &gt;string&lt;
reference: &gt;string&lt;
string: &gt;changed string&lt;
string reference: &gt;changed string&lt;
&lt;/code&gt;
&lt;p&gt;
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.
&lt;/p&gt;
&lt;p&gt;
Help, ideas, .. are greatly appreciated as I couldn't find anything suitable in the net or I'm looking for the wrong phrases. ;
&lt;/p&gt;
</field>
<field name="reputation">
12</field>
</data>
</node>
