Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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


In reply to Re: Specific hash to array conversion query by Zaxo
in thread Specific hash to array conversion query by monarch

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-03-28 13:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found