Simpler and easier to read: @combined = map { ref $_ eq 'ARRAY' ? @$_ : $_ } values %{$hash{family}};
Either dereference the array or return the scalar. -----Original Message----- From: Peter Scott [mailto:peter@;PSDT.com] Sent: Tuesday, October 22, 2002 10:51 AM To: [EMAIL PROTECTED] Subject: Re: using 'map' with mixture of scalars and array refs... In article <[EMAIL PROTECTED]>, [EMAIL PROTECTED] (Michael Kavanagh) writes: >Hi there, > >I've got a hash that has keys which contain either >- array references >- scalar values > >for example: >@values = (12398712984, 19286192879); >$hashofarrays{'family'}{'arrayref'} = \@values; >$hashofarrays{'family'}{'scalar'} = 12938712938; Well, it's not the keys that contain that, it's the values... >I'm trying to map the hash into another array that would just be a flattened >list of values. (a combination of the scalar values and the individual array >values) >I'm iterating through each key in the hash and then using 'map' on the key >to do this at the moment. > >I can get it to work for all the array references by mapping on the >de-referenced array ref, like this: > map { push @items, $_ } ( @{$hashofarrays{'family'}{'arrayref'} } >); That doesn't look like code that's iterating through the keys. You have a single hardcoded key. >But that ignores all the scalar values...it only gets the keys that contain >array references. > >To get the scalars, the following works: > map { push @items, $_ } ( $hashofarrays{'family'}{'scalar'} ); There's only one scalar there and hence the map is superfluous. >Is there an elegant / idiomatic way to do both at once? >I'm guess I could do it using a conditional that checks the hash value and >executes one of the above statements as I iterate through the hash keys, but >if theres a better way... @combined = map { @{$_->{arrayref}||[]}, exists $_->{scalar} ? $_->{scalar} : () } values %hashofarrays; -- Peter Scott http://www.perldebugged.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]