SPUG: A hash of hashes and arrays

Yitzchak Scott-Thoennes sthoenna at efn.org
Mon Aug 1 08:58:32 PDT 2005

On Mon, Aug 01, 2005 at 08:46:16AM -0700, Duane Blanchard wrote:
> Hi all,
> I have a hash of hashes and arrays. I'm able to print the all the keys
> of the outer hash, and print the embedded hashes and arrays
> individually, now I want to print the entire contents of the outer
> (matrix) hash.
> I need something like:
> for each key in the outer hash
> {   if (the key is a reference to a hash)

I think you mean the value, not the key.

>     {    print the contents of the hash;
>     }
>     elsif (the key is a reference to an array)
>     {    print the contents of the array;
>     }
> }
> I have everything in place, but don't know how to tell whether I'm
> looking at a hash or an array.

If there are no objects involved, this is easy.  See perldoc -f ref

while ( my ($key, $val) = each %outer_hash ) {
   if ( ref $val eq "HASH" ) {
   } elsif ( ref $val eq "ARRAY" ) {
   } else {
      warn "I don't think we're in Kansas anymore, Toto";

If there are objects involved, the problem is not generally solvable; you
have to decide what you will treat as a hash and what as an array.

More information about the spug-list mailing list