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

Re: hash of hashes question

Thread Previous
Jim Gibson
September 4, 2009 11:26
Re: hash of hashes question
Message ID:
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:


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

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About