Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Referencing to a hash inside a hash of hashes.

by JPaul (Hermit)
on Aug 08, 2001 at 00:39 UTC ( #102903=perlquestion: print w/ replies, xml ) Need Help??
JPaul has asked for the wisdom of the Perl Monks concerning the following question:

Yes, I know, it sounds like silliness, but honestly, I'm really trying to make this work.

my %hash; my %direc; my $record = { DIR => 3 }; $hash{'myhash'} = $record; $direc{'this'} = "something"; $direc{'that'} = "another"; $hash{'myhash'}->{ODIR} = %direc;
How do I get the contents of {ODIR}/%direc out?
I've tried:
$f = $hash{'myhash'}->{ODIR}{'this'}
$f = $hash{'myhash'}->{ODIR}=>{'this'}

Without any luck. I guess I just don't understand how perl puts together the hashes.

Thanks,
JP
-- Alexander Widdlemouse undid his bellybutton and his bum dropped off --

Comment on Referencing to a hash inside a hash of hashes.
Download Code
Re: Referencing to a hash inside a hash of hashes.
by TheoPetersen (Priest) on Aug 08, 2001 at 00:50 UTC
    Change the assignment of the ODIR key to a referencing operation:
    $hash{'myhash'}->{ODIR} = \%direc;
    Then dereference it as you had the 'myhash' key:
    $f = $hash{'myhash'}->{ODIR}->{'this'}
      Aha!
      Many thanks Theo,

      JP -- Alexander Widdlemouse undid his bellybutton and his bum dropped off --

Re: Referencing to a hash inside a hash of hashes.
by jepri (Parson) on Aug 08, 2001 at 00:58 UTC
    TheoPeterson is right, but he overlooks something:

    my %hash; my %direc; my $record = { DIR => 3 }; $hash{'myhash'} = $record; $direc{'this'} = "something"; $direc{'that'} = "another"; #Here we swat the previous value of $hash{'myhash'} #the previous value of $hash{myhash} ($record) vanishes $hash{'myhash'}{ODIR} = \%direc; print $hash{'myhash'}->{ODIR}{'this'},"\n"; print $hash{'myhash'}{ODIR}{'this'},"\n"; print $hash{'myhash'},"\n";

    In general, don't bother dereferencing with ->. It's not worth it unless you know why you want to.

    Also, you don't need the '' around the key values. They'll just cause you trouble later.

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.

      Also, you don't need the '' around the key values. They'll just cause you trouble later.

      They are unnecessary in this instance (and I'm lazy enough to leave out the quotes most of the time also), but I don't see how they'll cause trouble later. Actually, they'll save trouble if he decides to change the keys to something that is a perl keyword, or if perl changes and suddenly 'this', 'that' or 'myhash' becomes a perl keyword.

      Update:Hmm, tye right. runrig wrong (see below). Key words and function names don't cause problems, but how 'bout this:
      If you decide to add non-word characters later, it can cause trouble unless its quoted so it may save you from remembering to quote it. Or if you later decide to use a hash slice, you'll have to quote the arguments anyway. Or some people just think it looks better, so who am I to argue :)

        Actually, no. I don't see how they will cause problems later, but $hash{do}, $hash{time}, $hash{__PACKAGE__}, etc. are valid and use the keys "do", "time", and "__PACKAGE__", respectively (unless you have a really old copy of Perl).

                - tye (but my friends call me "Tye")
Re: Referencing to a hash inside a hash of hashes.
by rchiav (Deacon) on Aug 08, 2001 at 01:02 UTC
    You've got a couple problems here. 1st you can't do what you're looking to do they way you're looking to do it.

    When you assign like  $hash{'myhash'}->{ODIR} = %direc;
    .. there's 2 problems. One, it should be..  $hash->{'myhash'}{ODIR} = ...
    and second, it's not doing what you think it's doing.

    Without giving it away, put this in your script.

    print scalar %direc;
    That is exactly how the assignment is getting evaluated. It's not assigning the hash itself, it's assigning the results of the hash being evaluated in scalar context.

    What I think you want to do is assign a refrence to the hash as in

    $hash->{'myhash'}{ODIR} = \%direc;
    Now the key ODIR will contain a refrence to the hash %direc. And you'd print it like so..
    foreach (sort keys %{$hash->{myhash}{ODIR}}){ print "$_ => $hash->{myhash}{ODIR}{$_}\n"; }
    Hope this helps..
    Rich
Re: Referencing to a hash inside a hash of hashes.
by astaines (Curate) on Aug 08, 2001 at 02:35 UTC

    this may make it a little clearer

    #!d:\perl\perl my %hash; my %direc; my $record = { DIR => 3 }; $hash{'myhash'} = $record; print $hash{'myhash'},"\n"; ## Prints HASH(0x1b9f0b8) print $hash{'myhash'}{DIR},"\n"; ## Prints 3 $direc{'this'} = "something"; $direc{'that'} = "another"; #Here we swat the previous value of $hash{'myhash'} #the previous value of $hash{myhash} ($record) vanishes $hash{'myhash'}{DIR} = \%direc; print $hash{'myhash'}->{DIR}{'this'},"\n"; ## Prints something print $hash{'myhash'}{DIR}{'this'},"\n"; ## Prints something print $hash{'myhash'}->{DIR}{'that'},"\n"; ## Prints another print $hash{'myhash'},"\n"; ## Prints HASH(0x1b9f0b8) print $hash{'myhash'}{DIR},"\n"; ## Prints HASH(0x1b95778)
    The point about the quotes is that double quotes interpolate variable into their contents before passing them on. Single quotes don't.
    -- Anthony Staines

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2014-12-25 06:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (159 votes), past polls