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

Re: [FWP] nested lists of hierarchical structures



John Porter <jdporter@min.net> wrote:
>#
># convert the dir names into a tree:
>#
>my $root = {};
>for ( @dirs ) {
>  my $tree = $root;
>  for ( split '/' ) {
>    $tree = $tree->{$_} ||= {};
>  }
>}

Here's a tree-building algorithm that I like to use that depends on
autovivification:

foreach (@dirs) {
	my $node = \\%root;
	$node = \$$node->{$_} foreach split '/';
}

I'm not sure how the speeds might compare, but one advantage is that it
doesn't leave an empty hash hanging off of every leaf.

(I must confess that whenever I write code like this, for a moment I have
to fight the irrational urge to write the last assignment as
'$node = \$$$node{$_}'.)

-- 
Sean McAfee                                                mcafee@umich.edu
print eval eval eval eval eval eval eval eval eval eval eval eval eval eval
q!q@q#q$q%q^q&q*q-q=q+q|q~q:q? Just Another Perl Hacker ?:~|+=-*&^%$#@!

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