That is a great problem i.e. I found it a lot more difficult than it first appeared. I'm not sure I'd call this solution elegant, although it would look better if the code is cleaned up (a lot). I'd clean it up myself, but it's taken me most of this afternoon to work this out and the boss will have my hide if he finds out how little I've produced today.
#!/usr/bin/perl -w use strict; use Dumpvalue; # example structure my $struct = { fruit => [qw( apple pear )], type => [qw( organic farmed )], period => { 20050824 => { to => [ 'new york', 'l +ondon', ], transport => 'air' }, 20050825 => { to => 'auckland', }, } , name => 'bangers', }; #logHere $struct; print "Input:\n"; Dumpvalue->new->dumpValue( $struct ) ; my $expanded = expand($struct, []); print '='x80,"\nResult:\n"; Dumpvalue->new->dumpValue( $expanded ) ; sub expand { my ( $struct, $expanded, $parent_key) =@_; for my $key ( sort keys %$struct ) { if ( ref($struct->{$key}) eq 'ARRAY' ) { if ( scalar @$expanded ) { $expanded = [ map { my $row = $_; map { my $newrow = {%$row}; +$newrow->{$key} = $_; $newrow } @{$struct->{$key}} } @$expanded ]; } else { push @$expanded, map { { $key=> $_ } } @{$struct->{$key}} ; } } elsif ( ref $struct->{$key} eq 'HASH' ) { $parent_key ||=''; my $expansion = expand($struct->{$key}, [], $key); $expansion = [map { {%$_, $parent_key => $key} } @$expansion] +if $parent_key; if ( scalar @$expanded ) { my $count = scalar @$expanded; $expanded = [ map { my $row = $_; map { my $newrow = {%$row}; @$newrow{keys %$_} = values %$_ unle +ss $newrow->{$parent_key}; $newrow } @$expansion ; } @$expanded ]; push @$expanded, @$expansion if $count == scalar @$expanded; } else { push @$expanded, @$expansion ; } } else { if ( scalar @$expanded ) { map { $_->{$key} = $struct->{$key} } @$expanded; } else { push @$expanded, { $key => $struct->{$key} }; } } } return $expanded; }

In reply to Re: Expanding / flattening a structure by bangers
in thread Expanding / flattening a structure by jaa

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.