I am receiving a hash of hashes from another function, and some elements of the hash of hashes can be another hash. How can I test to see if something is a hash?
The exists() function in Perl is used to check whether an element in an given array or hash exists or not. This function returns 1 if the desired element is present in the given array or hash else returns 0.
Perl Hash Accessing To access single element of hash, ($) sign is used before the variable name. And then key element is written inside {} braces.
This information can be found by typing perldoc perlref at the command line. Single element slice better written as %{$_[0]} , %{shift} is referring to a hash variable named shift , you probably meant %{+shift} or %{shift @_} .
When a hash is in LIST context Perl converts a hash into a list of alternating values. Each key-value pair in the original hash will become two values in the newly created list. For every pair the key will come first and the value will come after.
Depending on what you want you will need to use ref
or reftype
(which is in Scalar::Util
, a core module). If the reference is an object, ref
will return the class of the object instead of the underlying reference type, reftype
will always return the underlying reference type.
if (ref $var eq ref {}) { print "$var is a hash\n"; } use Scalar::Util qw/reftype/; if (reftype $var eq reftype {}) { print "$var is a hash\n"; }
Use ref
function:
ref($hash_ref) eq 'HASH' ## $hash_ref is reference to hash ref($array_ref) eq 'ARRAY' ## $array_ref is reference to array ref( $hash{$key} ) eq 'HASH' ## there is reference to hash in $hash{$key}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With