Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Organizing data from a hash

by jdporter (Paladin)
on Dec 01, 2020 at 18:26 UTC ( [id://11124466]=note: print w/replies, xml ) Need Help??


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();
I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11124466]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (1)
As of 2024-04-25 03:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found