Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Dynamically building a hash of hashes

by willdooUK (Beadle)
on Jul 12, 2001 at 16:09 UTC ( [id://95993]=perlquestion: print w/replies, xml ) Need Help??

willdooUK has asked for the wisdom of the Perl Monks concerning the following question:

Apologies if this is going over old ground, but I can't find the answer to this in the archives.

I'm trying to dynamically build a hash of hashes, but I can't seem to get the context right. Here's the test code I'm working on:
my %attrs = ( name => 'Band', link => 'band.html'); print "ATTRS KEYS = ", keys %attrs, "\n"; print "ATTRS VALUES = ", values %attrs, "\n"; my %container = (); $container{ATTRIBUTES} = %attrs; print "CONTAINER KEYS = ", keys %container, "\n"; print "CONTAINER VALUES = ", values %container, "\n"; my %new_attrs = $container{ATTRIBUTES}; print "NEW_ATTRS KEYS = ", keys %new_attrs, "\n"; print "NEW_ATTRS VALUES = ", values %new_attrs, "\n";
What I want is for the %new_attrs hash to be loaded with the hash that I put into %container, but it only wants to give me a garbage reference. If I try the line like this:
my %new_attrs = %{$container{ATTRIBUTES}};
... I get nothing out at the end (other than a Can't use string ("1/8") as a HASH ref while "strict refs" error if strict is in use).

Any help would be much appreciated.

willdooUK
--------------
"Home is a castle you built in my mind; I'm home anywhere, anytime."
Donny Hathaway

Replies are listed 'Best First'.
Re: Dynamically building a hash of hashes
by japhy (Canon) on Jul 12, 2001 at 16:11 UTC
    You're storing %hash in scalar context in the other hash, instead of storing a reference to it:
    $container{ATTRIBUTES} = %attrs; # should be $container{ATTRIBUTES} = \%attrs;
    That will make your de-referencing code (which currently says "1/8" isn't a valid hash reference) work.

    japhy -- Perl and Regex Hacker
      Fantastic!

      That's the kind of answer I like (just one little character and its fixed).
      Interestingly enough, the new hash has also been re-ordered...

      willdooUK
      --------------
      "Home is a castle you built in my mind; I'm home anywhere, anytime."
      Donny Hathaway

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-03-28 08:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found