At 1:56 PM -0700 9/4/09, Noah Garrett Wallach wrote:
Hi there,

I am trying to figure out how to use hash of hashes properly. can values and keys be at the same level?

I am running in to troubles. Maybe values and keys cant be at the same level ?

Values and keys exist as pairs within a hash. They must always be at the same "level". Hashes (and arrays) don't really have "levels", as there is only one set per hash (or array).

You can have a hash value that is a reference to another hash, thereby creating multi-level data structures. See 'perldoc perldsc' for details. You can freely mix hashes and arrays by creating references to the appropriate structures, but it is up to you to keep track of what is what at each level and branch and access the elements appropriately.

                $policy{policy_statement}{$key} = 2;

This sets the value of $policy{policy_statement}{$key} to the scalar value 2.

                $policy{policy_statement}{$key}{group_name}{$1} = 1;

This assumes that the value of $policy{policy_statement}{$key} is a reference to a hash, which it is not. Hence the runtime error message. The value of $policy{policy_statement}{$key} is 2, which cannot be dereferenced to yield a hash.

                $policy{policy_statement}{$key}{policy_type}{$2} = 1;

the perl error results

Can't use string ("2") as a HASH ref while "strict refs" in use at ./policy.sanitizer line 174.

You must be consistent and decide how many levels your hash-of-hashes will have and always use the appropriate amount of dereferencing.

Use the Data::Dumper module to print a multi-level data structure and reveal its structure and contents:

        use Data::Dumper;

        print Dumper(\%policy);

--
Jim Gibson
jimsgib...@gmail.com

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to