Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: is it an array?

by rkrieger (Friar)
on Jan 20, 2010 at 07:58 UTC ( #818391=note: print w/replies, xml ) Need Help??

in reply to is it an array?

If your key contains an array, the hash value is a reference to that array. Testing with ref should tell you what sort of value it is.
foreach my $key (keys %hash) { $value = $hash{$key}; # Perhaps a dispatch table for this would be better... if (ref($value) eq q{ARRAY}) { # Do whatever you you feel like doing with your array } elsif (ref($value) eq q{}) { # Scalar, handle as you wish } else { # None of the above types; make sure you know } }

The different sorts of values (see perlfunc for ref and its use) allow you to handle more than scalar (strings) or arrays. It may pay to know if your data contains something unexpected instead of just trying to parse it.

If handling multiple types, a dispatch table allows for easier extension. Handling the 'other data type' case is different, but a simple defined() usually does it for me.

A caveat to the above is probably if you have an object (and a reference) that you blessed into the ARRAY class. I suspect that's why ikegami above mentioned Scalar::Util's reftype().

Edit: Fixed the scalar case, since ref() returns an empty string for a non-reference (and SCALAR for a reference to a scalar value). Thanks to a kind monk paying attention.

Replies are listed 'Best First'.
Re^2: is it an array?
by ack (Deacon) on Jan 20, 2010 at 16:41 UTC

    From my perspective, this is an excellent response. I was struggling a bit by the OP's comment that

    "each key may or may not be an array."

    since an array as a hash key uses the stringified reference address as the key rather than any of the array's constituents.

    It seemed like the OP wanted the contents of the array to somehow be the key(s). But I may have not been properly understanding what the OP was seeking. I always get a bit nervous when someone uses either array or hash references as 'keys' to hashes or as indecies for arrays.

    Just MHO, YMMV.

    ack Albuquerque, NM

      This is probably going off-topic. From the LDIF structure displayed and the mention of %ldif, I assumed the keys are 'sn', 'mail', etc. What the keys are, rather, would correspond to the contents of $ldif{'sn'}.

      Since $ldif{$key} either contains a scalar value or (a reference to) an array, this seemed the simplest way to fit things together. I can see the use in having the attributes for an LDAP entry as keys in your hash.

      I don't directly see much point on the contents being useful as keys to an array, but that doesn't mean that there couldn't be any :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://818391]
and dust plays in a shaft of sunlight...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (10)
As of 2017-07-25 19:17 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (377 votes). Check out past polls.