Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Specific hash to array conversion query

by Zaxo (Archbishop)
on Jul 12, 2005 at 03:06 UTC ( [id://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,
Zaxo

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-24 00:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found