my $string = join ",", sort map { $_->{cat_name} } values %$VAR1;
See also perldsc, perlreftut, and perlref.
Sorry, I missed the part about the parent ID. Update forthcoming, please hold.
Update: Ok, here's one of several ways to do it, using a topological sort, assuming this graph representation of your categories makes sense - you'd have to explain your data structure more otherwise.
use warnings;
use strict;
use Graph;
my $data = {
'986172' => {
'cat_parent_id' => '',
'cat_name' => 'Category1'
},
'986178' => {
'cat_parent_id' => '986177',
'cat_name' => 'Category4'
},
'986177' => {
'cat_parent_id' => '986176',
'cat_name' => 'Category3'
},
'986176' => {
'cat_name' => 'Category2',
'cat_parent_id' => '986172'
}
};
my $g = Graph->new(directed => 1);
for my $k (keys %$data) {
next unless $data->{$k}{cat_parent_id};
die unless exists $data->{ $data->{$k}{cat_parent_id} };
$g->add_edge($data->{$k}{cat_parent_id}, $k);
}
my $string = join ",", map { $data->{$_}{cat_name} }
$g->topological_sort;
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.