# Print childs for a specific albumid sub print_album_childs{ my ($albums, $parent_albumid, $level) = @_; if(defined($level)){ $level++; } else { $level = 1; } foreach my $albumid (keys %$albums){ if($albums->{$albumid}->{parent}){ # we have an album with a parent defined if($albums->{$albumid}->{parent} == $parent_albumid){ # we have a child for the parent album # print it + all of it's childs print_album_line($albums, $albumid, $level); # Look for more childs recursively print_album_childs($albums, $albumid, $level); } } } return 1; # stop recursion/subroutine, not really needed } ## ## Somewhere else in the script ## # Let's find all the childs for this album print_album_childs($albums, $albumid); ## ## Example output ## albumid name description parent added ------------------------------------------------------------------------------------------------------------------------------------------------- 4 asdf3f23 23f 23f2 f23 f23 f23f 2 [...] gasgasdg asdg as dg asdg sa - 2013-10-09 00:32:10.151894+02 > 6 test2 test2 4 2013-10-15 00:23:34.913566+02 >> 9 test6 test6 6 2013-10-15 00:24:09.479081+02 > 12 test123 test123 4 2013-10-15 00:25:03.870669+02 > 5 test1 test1 4 2013-10-15 00:06:46.638321+02 >> 7 test3 test3 5 2013-10-15 00:23:51.946641+02 >> 8 test4 test4 5 2013-10-15 00:23:58.846858+02 >>> 11 test12 test12 8 2013-10-15 00:24:48.48431+02 >>> 10 test11 test11 8 2013-10-15 00:24:41.140765+02 13 asdf2324 asdfasdfasdf - 2013-10-15 00:45:27.638672+02 14 g4gg34g g2g2g2g - 2013-10-15 00:45:32.643721+02 -------------------------------------------------------------------------------------------------------------------------------------------------