in reply to Expanding / flattening a structure
#!/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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Expanding / flattening a structure
by jaa (Friar) on Aug 25, 2006 at 15:08 UTC |