title: get all nodes from multi level hash in perl
body: <p>I want all nodes of each key sorted by key in hash ref or array or something like so that I can iterate that according to my need since I have to display each key with its all children. 
  following is my data structure:</p>
  
  <pre><code> $hash1 = {
            '3' =&gt; {                  
  
                     'title' =&gt; 'Parent-3', 
                      'parentid' =&gt; '-1'               
                   },
            '1' =&gt; {
  
                     'children' =&gt; {
                                     '11' =&gt; {                                           
                                               'title' =&gt; 'child-1',                                            
                                             },
                                     '5' =&gt; {
  
                                              'children' =&gt; {
                                                              '8' =&gt; {                                                                   
                                                                       'title' =&gt; 'first child of child-2',                                                                    
                                                                     },
                                                              '13' =&gt; {                                                                     
                                                                        'title' =&gt; 'second child of child-2',                                                                    
                                                                      }
                                                            },
                                              'title' =&gt; 'child-2',                                          
                                            }
                                   },
                     'title' =&gt; 'Parent-1', 
                      'parentid' =&gt; '-1'                
                   },
            '2' =&gt; {                 
                     'title' =&gt; 'Parent-2',  
                      'parentid' =&gt; '-1'              
                   },
            '4' =&gt; {                 
                     'title' =&gt; 'Parent-4',
                     'parentid' =&gt; '-1'
                   },
          };
  </code></pre>
  
  <p>I have used following functions :</p>
  
  <pre><code>sub Options {
  
      my $hash = shift;
      my $options = '';
  
      my $iter; $iter = sub {     
          my $hash = shift;
          my $indent = shift || '';
          foreach my $k (sort {$a &lt;=&gt; $b} keys %{$hash}) {            
              my $v = $hash-&gt;{$k};                
              if($v-&gt;{parentid} eq '-1'){
              $options .= $v-&gt;{title} ."-parent\n";           
              }else{
              $options .= $v-&gt;{title} . "," ;
              }
              if ($v-&gt;{children}){
                   $iter-&gt;($v-&gt;{children}, $indent . "");             
              }                   
          }
          chop($options);
          $options .= "\n";
      };
  
      $iter-&gt;($hash);     
      return $options;
  }
  </code></pre>
  
  <p>here it returns a string with comma separated but I need some kind of data structure so that I can able to find all children for each key (in the form of hash ref or array) like:</p>
  
  <pre><code>Parent-1 -&gt; [child-1,child-2, first child of child-2, second child of child-2]
  Parent-2
  Parent-3
  Parent-4
  </code></pre>
  
  <p>any one can help me out? thanks in advance.</p>