Greetings, Monastery denizens:
I've been struggling with this seemingly simple problem for a while, and would really appreciate some input from brains that are not mine.
I have a data structure of arbitrary depth handed to me that is composed of hash/arrayrefs for which I need to create a path-type lookup. This is proving to be a pain, since I have to figure out what the structure is at every level - so I would really rather flatten it to hashrefs all the way down. The main fact here is that every hashref has a 'name' key in it pointing to a text value - so that's how I'd like to "re-key" this thing.
Example:
$foo = {
[
{
name => 'a',
type => 1,
rockets => 2,
leaves => 3
},
{
name => 'b',
type => 7,
rockets => 4,
leaves => 1,
samples => [
{
name => 'mary',
dog => 'fifi'
},
{
name => 'john',
fish => 'oscar'
}
]
}
]
};
And here's what I'd like to produce (I don't care whether the { 'name' => 'value' } entry remains in the result or not):
$foo1 = {
a => {
name => 'a',
type => 1,
rockets => 2,
leaves => 3
},
b => {
name => 'b',
type => 7,
rockets => 4,
leaves => 1,
samples => {
mary => {
name => 'mary',
dog => 'fifi'
},
john => {
name => 'john',
fish => 'oscar'
}
}
}
};
Thanks in advance for the help!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.