Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need to find out whether or not the value inside of a particular hash is a scalar or an anonymous array. Can somebdy tell me a quick way to find this, since both have to be processed differently...?

Thanks

Here's a sample of the printed hash...

bob => 150lbs times_he_ate_cheeseburgers => ARRAY(0xd2a2c) favorite_night_spots => ARRAY(0xc7100) ex_girlfriends => ARRAY(0xc7148) favorite_pokemon => ANY best_friend => DOG

Replies are listed 'Best First'.
Re: Finding variable context
by Hot Pastrami (Monk) on Sep 29, 2000 at 20:58 UTC
    Use the ref() funtion...
    if (ref($array[$i]) eq "ARRAY") { print "It's an array!"; } else { print "Not an array!"; }

    Update: Oops, you said in a hash, my bad... here's a better example:
    if (ref $hash{$key} eq "ARRAY") { print "It's an array ref!"; } else { print "Not an array ref!"; }

    Alan "Hot Pastrami" Bellows
    -Sitting calmly with scissors-
Re: Finding variable context
by tye (Sage) on Sep 29, 2000 at 21:57 UTC

    Please instead use:

    if( UNIVERSAL::isa( $hash{$key}, "ARRAY" ) ) { dosomething( @{ $hash{$key} } ); } else { dosomething( $hash{$key} ); }

    This makes your code work for non-ordinary array references.

            - tye (but my friends call me "Tye")
Re: Finding variable context
by swiftone (Curate) on Sep 29, 2000 at 21:05 UTC