In support of F. Xavier Noria, and in simpler terms - your $vars = {
..... } overwrote your previous assignment of $vars->{'key2'}.

Perhaps you could have done:

my $var = {};
$var->{'key2'} = "some value";
my @args = qw/ XXX YYY ZZZ /;
my @vals = qw/ AAA BBB CCC /;
my $i;
for ($i =0; $i < scalar(@args); $i++) {
  $vars->{$args[$i]} = $vals[$i];
}
$var->{'key1'} = "some other value";

This would not have overwritten the $var->{'key2'} assignment.

--Jon Robison


"F.Xavier Noria" wrote:
> 
> On Fri, 17 May 2002 17:10:53 +0300 (EEST)
> Viljo Marrandi <[EMAIL PROTECTED]> wrote:
> 
> : $vars->{'key2'} = "value of second key";
> 
> The hash $vars points to has a key named "key2".
> 
> : $vars = {
> :     xxx => "AAA",
> :     yyy => "BBB",
> :     zzz => "CCC",
> : };
> 
> Now you change the reference stored in $var. It points to an entirely
> new hash, whose keys are "xxx", "yyy" and "zzz".
> 
> : $vars->{'key1'} = "value of first key";
> 
> Here you add the key "key1" to the hash $vars points to.
> 
> : Problem is, that value of key2 is lost after I set values to xxx, yyy and
> : zzz, but key1 is ok.
> 
> $vars contains a reference to a hash that has nothing to do with the
> first one, you didn't create a key named "key2" in that hash.
> 
> -- fxn

Reply via email to