Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: Specific hash to array conversion query

by Zaxo (Archbishop)
on Jul 12, 2005 at 03:06 UTC ( #474158=note: print w/replies, xml ) Need Help??

in reply to Specific hash to array conversion query

Interesting challenge to build that with map. The difficulty is that the expansions will need to be somewhat inside-out. Lets try building it one step at a time, outside-in.

Given %properties, we need to work first with its keys, since their names are part of the output. So, my @values = map { foo( $_) } keys %properties; where foo() takes a key of %properties and returns a list of array references taken from that key.

Making an actual sub foo() for that is probably not justified, so we'll expand foo's definition in place,

my @values = map { my $key = $_; map { bar( $key, $_) } keys %{$properties{$_}} } keys %properties;
bar() is just like foo(), but it has more arguments to help build our array references.

In fact, bar() has all the arguments we need, so we expand it again,

my @values = map { my $key = $_; map { [ $key, $_, $properties{$key}{$_}{'value'} ] } keys %{$properties{$_}} } keys %properties;
There it is, with map, all in one statement.

That doesn't bother to sort the keys, but if you want that, you can insert it,

my @values = map { my $key = $_; map { [$key, $_, $properties{$key}{$_}{'value'}] } sort {$a <=> $b} keys %{$properties{$_}} } sort keys %properties;

It's a good idea to avoid one-lining constructions like this. It makes them very hard to read if you skip indentation.

After Compline,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://474158]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (10)
As of 2020-05-29 20:36 GMT
Find Nodes?
    Voting Booth?
    If programming languages were movie genres, Perl would be:

    Results (171 votes). Check out past polls.