First, the data structure you've shown isn't actually valid: $foo = { [ ... ] }; is missing a hash key, so for now I'm just going to assume $foo = { foo => [ ... ] };

I need to create a path-type lookup

You may be interested in Data::Diver. I also showed some custom "diver" type code here and here.

Also, it's unclear to me if your data structure always alternates between array and hash refs? Or can you ever have a hash of hashes? If not, this code will work on the sample data you showed, but it does make some assumptions about the structure of the input data. If you need to differentiate between arrays and hashrefs, you'll have to add things like if (ref $e eq 'ARRAY') { ... } elsif (ref $e eq 'HASH') { ... }.

sub rekey { my $h = shift; for my $k (keys %$h) { next unless ref $$h{$k}; my %n; for my $e (@{$$h{$k}}) { die "duplicate key '$$e{name}'" if exists $n{$$e{name}}; $n{$$e{name}} = $e; rekey($e); } $$h{$k} = \%n; } } rekey( $foo );

Update: You might also want to take a look at Data::DPath or Data::Path, I haven't used these myself but they sound like they might be fitting.

Update 2: Fixed typo in text.


In reply to Re: Recursive data structure munging - arrayrefs to hashrefs by haukex
in thread Recursive data structure munging - arrayrefs to hashrefs by flightdm

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



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.