Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

how do I use a hash of hashes reference in a function ?

by Anonymous Monk
on Sep 22, 2000 at 21:44 UTC ( #33662=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This is a rephrased version of my previous question.
I can pass a hash of hashes to a function OK:
$hash{name} = {date => "00-22-00", city=>"Chicago",}; function (\%hash1); function (\%hash2);

However I've tried several examples given previously and none work completely
sub function { my %hashref=%{shift()}; #the below works ok print $hashref{name}{city},"\n"; # but adding to the hash doesn't work if I print out # the entire hash outside this function $hashref{name}={data1=>"moredata",}; # it does work if I print it out inside the function }

Comment on how do I use a hash of hashes reference in a function ?
Select or Download Code
Re: how do I use a hash of hashes reference in a function ?
by merlyn (Sage) on Sep 22, 2000 at 21:53 UTC
    $hashref{name}={data1=>"moredata",};
    Wrong syntax for adding to a hash. Use this:
    $hashref{name}{data1} = "moredata";
      Just for clarification (for the original poster), your syntax:
      $hashref{name} = { data1 => "moredata" };
      *replaces* the reference in $hashref{name} with a new one, composed of your data1 => "moredata" hash. That's why it won't work. merlyn is correct in that if you wish to modify the hash referenced by $hashref{name} you don't do that by setting $hashref{name}, you modify it as if it were a hash: $hashref{key}{subkey} = $xyz;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://33662]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2014-09-22 01:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (177 votes), past polls