in reply to Creating Hash of hash of hash dynamically
But lets say that you REALLY want a tree structure for this kind of thing:
sub add_line { my ($tree,@line) = @_; my $first_element = shift @line; if (is_a_folder_title($first_element)) { # TODO: implement is_a_fol +der_title() $tree->{$first_element} ||= {}; # create empty hashref if none ex +ists, yet add_line($tree->{$first_element},@line); } else { $tree = [ $first_element, @line ]; } }
To get the values out again:
If you have a seperate list of fields and folders, it's a lot easier to use a "simple" hash of arrays instead:sub get_elements { my ($tree,@folder_names) = @_; return $tree unless @folder_names; my $subtree = $tree->{shift(@folder_names)}; return get_elements($subtree,@folder_names); }
$directories{ join("/",@folder_names } = \@fields; my @fields = @{ $directory{ join("/",@folder_names) } };
|
|---|