in reply to Referencing to a hash inside a hash of hashes.

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.

Replies are listed 'Best First'.
Re: Re: Referencing to a hash inside a hash of hashes.
by runrig (Abbot) on Aug 08, 2001 at 01:04 UTC
    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")