develooper Front page | perl.beginners | Postings from September 2009

Re: hash of hashes question

Thread Previous
From:
Jim Gibson
Date:
September 4, 2009 11:26
Subject:
Re: hash of hashes question
Message ID:
p06240800c6c70927ac2b@[192.168.1.3]
At 11:02 AM -0700 9/4/09, Noah Garrett Wallach wrote:
>Hi there,
>
>I am having some trouble understanding hash of hashes here.   I want 
>to find all the keys for %policy{'policy_statement'}
>
>     for my $line (@lines) {
>         for my $key ( keys %policy{'policy_statement'} ) {


%policy is a hash.

$policy{policy_statement} (quotes not needed in this case) is a 
scalar value of the element with key 'policy_statement'. The scalar 
value is a reference to a hash.

Therefore, %{$policy{policy_statement}} is the hash to which this 
value refers. You can occasionally leave out the outer set of braces, 
but not in this case.

So change the above to

  for my $key ( keys %{$policy{'policy_statement'}} ) {

Elements of the second-level hash and deeper hashes may be referred 
to with the following:

   $policy{key1}->{key2}
   $policy{key1}->{key2}->{key3}

etc. In this case you may leave out the -> symbols.


>             if ($line =~ 
>/set\sprotocols\sbgp\sgroup\s(\S+)\s(import|export).*?$key\s/) {
>                 $policy{'policy_statement'}{$key} = 2;
>                 $policy{'policy_statement'}{$key}{'group_name'}{$1} = 1;
>                 $policy{'policy_statement'}{$key}{'policy_type'}{$2} = 1;


The rest looks OK, and compiles.


-- 
Jim Gibson
Jim@Gibson.org

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About