[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] nested lists of hierarchical structures



Matija Grabnar wrote:
> I had a sorted bunch of newsgroup names, and I wanted to format them 
> as a list of lists where each newsgroup would be as deep in 
> the list as it was in the hieararchy.


#
# convert the dir names into a tree:
#
my $root = {};
for ( @dirs ) {
  my $tree = $root;
  for ( split '/' ) {
    $tree = $tree->{$_} ||= {};
  }
}


#
# now that we have the tree, one of the things we 
# can do with it is walk it in depth-first fashion,
# printing things out as we go, with each level
# indented appropriately.
#
$" = '/';
dfs_walk( $root );
sub dfs_walk {
  my( $hr, @path ) = @_;
  my $pre = '  ' x @path;
  print "$pre<li> @path\n" if @path;
  for ( sort keys %$hr ) {
    print "$pre<ul>\n";
    dfs_walk( $hr->{$_}, @path, $_ );
    print "$pre</ul>\n";
  }
}



-- 
John Porter

"There are people who have fun studying the C++ spec for
literally seconds on end, I'm sure..."    John Vlissides


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe