in reply to Complex hash?

if you don't care what hash key order, you can do simply:

my @values = map { (ref $_ eq 'ARRAY') ? @$_ : $_ } values %hash;

if you do care, this works:

my @values = map { (ref $hash{$_} eq 'ARRAY') ? @{$hash{$_}} : $hash{$_} } sort keys %hash;

Replies are listed 'Best First'.
Re^2: Complex hash?
by tamaguchi (Pilgrim) on Aug 24, 2006 at 22:29 UTC
    if (ref($hash{$_}) eq 'ARRAY')

    I dont understand this.. Does the word 'ARRAY' have a special meaning in this context? What more such special words are there, and where is it possible to read about this? Appearently it does not work to write 'array' in lc.
      The reason that 'array' doesn't match is that you are comparing the return value of ref with the string 'array'. ref returns what sort of reference something is, and in the case of arrays that string is 'ARRAY'

      See ref for more information.

      ref returns the following values:
      • SCALAR
      • ARRAY
      • HASH
      • CODE
      • REF
      • GLOB
      • LVALUE
      And it those that you must test for - depending on what you are looking for.

        Just to disambiguate a little: ref returns SCALAR for a reference to a scalar. For a simple scalar it returns undef.

        Note too that ref returns the object type for an object so the range of strings that can be returned is essentially unlimited.


        DWIM is Perl's answer to Gödel