in reply to Re: Expanding / flattening a structure
in thread Expanding / flattening a structure
Oh Fabulous Bangers! Thank you very much!
Exactly what I was looking for - and I can now see how you managed to stitch together a mild case of recusion... and the embeded map/map cartesian product - rather nifty indeed!
I have made one slight performance tweak:
to:my $count = scalar @$expanded; $expanded = [ map { my $row = $_; map { my $newrow = {%$row}; @$newrow{keys %$_} = values %$_ unless $newrow->{$parent_key}; $newrow } @$expansion ; } @$expanded ]; push @$expanded, @$expansion if $count == scalar @$expanded;
as this tests for the presence of $parent_key once, rather than using the before/after count to decide if all the children are new.if ( $expanded->[0]{$parent_key} ) { push @$expanded, @$children; } else { # create cartesian product against existing $expanded $expanded = [ map { my $row = $_; map { my $newrow = {%$row}; # clone expanded row @$newrow{keys %$_} = values %$_; # add in parent_key details $newrow # return new row } @$children; } @$expanded ]; }
Mucho kudos! and many many thanks
Jeff
|
|---|