Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Fun little problem - just stick the parents in front of the children and clean it up at the end :)

#!/usr/bin/perl use strict; use warnings; use List::Util qw( uniq ); my $data = { 986172 => { cat_name => "Category1", cat_parent_id => "" }, 986176 => { cat_name => "Category2", cat_parent_id => 986172 }, 986177 => { cat_name => "Category3", cat_parent_id => 986176 }, 986178 => { cat_name => "Category4", cat_parent_id => 986177 }, }; my $string = join ',', find( keys %$data ); print "$string\n"; sub find { uniq map { my $ref = $data->{$_}; $ref ? ( find( $ref->{cat_parent_id} ), $ref->{cat_name} ) : () } @_; }

Or build a little prerequisite table and topologically extract the names one by one.

#!/usr/bin/perl use strict; use warnings; my $data = { 986172 => { cat_name => "Category1", cat_parent_id => "" }, 986176 => { cat_name => "Category2", cat_parent_id => 986172 }, 986177 => { cat_name => "Category3", cat_parent_id => 986176 }, 986178 => { cat_name => "Category4", cat_parent_id => 986177 }, }; my $parents = ''; for my $v ( values %$data ) { $parents .= $v->{cat_parent_id} ? $v->{cat_name} . ' ' . $data->{$v->{cat_parent_id}}{cat_name} . + "\n" : $v->{cat_name} . "\n"; } #print "$parents\n"; my @order; $parents =~ s/\b($1)\b//g, push @order, $1 while $parents =~ s/^(\w+)\ +h*\n//m; my $answer = join ',', @order; print "$answer\n"; $parents and die "loop in parents at\n$parents";

In reply to Re: Organizing data from a hash by tybalt89
in thread Organizing data from a hash by audioboxer

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (6)
    As of 2021-04-19 20:22 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?