in reply to Organizing data from a hash
Using a module is usually a good idea. But for fun, here's my hand-rolled solution.
my $ds = { '986172' => { 'cat_parent_id' => '', 'cat_name' => 'Category +1', }, '986178' => { 'cat_parent_id' => '986177', 'cat_name' => 'Category +4', }, '986177' => { 'cat_parent_id' => '986176', 'cat_name' => 'Category +3', }, '986176' => { 'cat_parent_id' => '986172', 'cat_name' => 'Category +2', } }; # invert the structure: my $sd = { map { $ds->{$_}{'cat_parent_id'} => { 'cat_child_id' => $_, + 'cat_name' => $ds->{$_}{'cat_name'} } } keys %$ds }; sub foo # recursive { local $_ = shift || ''; exists $sd->{$_} ? foo( $sd->{$_}{'cat_child_id'}, @_, $sd->{$_}{' +cat_name'} ) : @_ } print join ',', foo();
|
|---|