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.


In reply to Re: is it an array? by rkrieger
in thread is it an array? by vlashua

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.