in reply to Re: trees of word lists
in thread trees of word lists

Every day I realize I have more to learn. Your code is much more elegant than my recursive solution.

use warnings; use strict; use Data::Dumper; my @lists = ([qw/Truck Car Bike/], [qw/Truck Car Scooter/], [qw/Truck Trailer Skates/]); my %tree; for my $list (@lists) { to_tree(\%tree, $list); } print Dumper(\%tree); sub to_tree { my ($head, $list) = @_; my $node = shift @$list; return unless defined $node; $head->{$node} ||= {}; to_tree($head->{$node}, $list); }

Replies are listed 'Best First'.
Re^3: trees of word lists
by MidLifeXis (Monsignor) on Jan 23, 2009 at 19:39 UTC

    I am not even sure I would call your code inelegant. I find it very easy to understand, and very much what I would have written.

    --MidLifeXis