Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

is it an array?

by vlashua (Initiate)
on Jan 20, 2010 at 04:52 UTC ( [id://818366]=perlquestion: print w/replies, xml ) Need Help??

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

data is ldif ...
hash is structure:
objectclass => a,b,c,d
sn => "Lashua"
mail => "vlashua@thnktnk.net"
... and many more

each key may or may not be an array

After parsing a file into %ldif, how do I find which keys contain arrays? Isn't there an isarray function ... it's been a while and I can't find it if there is.

if ... @{$ldif{$key}} || exists || =~ /ARRAY/ || ...

throw errors or don't work!
Help me, masters, with something very simple, I'm sure.
Thanks.

Replies are listed 'Best First'.
Re: is it an array?
by ikegami (Patriarch) on Jan 20, 2010 at 05:04 UTC
    if (defined($x) && eval { @$x || 1 }) { print "\$x can be used as an array ref (@$x)\n"; } else { print "\$x cannot be used as an array ref\n"; }

    You could also use Scalar::Util's reftype,

    if ((reftype($x) || '') eq 'ARRAY') { print "\$x is an array ref (@$x)\n"; } else { print "\$x is not an array ref\n"; }

    They are not equivalent. Note the difference in the messages. The first method checks if something can be used as an array reference while the second only checks if it actually is an array reference.

Re: is it an array?
by rkrieger (Friar) on Jan 20, 2010 at 07:58 UTC
    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.

      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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://818366]
Approved by ikegami
Front-paged by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-19 21:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found