in reply to List Values As Multidimensional Hash Keys

Basicaly, you're building a tree structure, where the leafs contain the data, and the brancehs are labeled by the parts of the key.

Something like this might do the trick:

#!/usr/bin/perl -w my $tree={}; # warning: recursive subroutine sub insert { my ($tree,$val,@keys)=@_; my $key=shift @keys; unless (defined($tree->{$key})) { $tree->{$key}={}; } if (scalar @keys) { insert($tree->{$key},$val,@keys); } else { $tree->{$key}=$val; } } insert($tree,'1',qw(a b c d)); # test 1 insert($tree,'2',qw(a b d e)); # test 2 insert($tree,$val,split(':',$key); # the call you were looking for.

Replies are listed 'Best First'.
Re: Re: List Values As Multidimensional Hash Keys
by simonm (Vicar) on Mar 15, 2004 at 17:16 UTC
    I've got a canned implementation like this on CPAN, Data::DRef, which will also do the key splitting for you:
    use Data::DRef ( set_value_for_key ); $Data::DRef::Separator = ':'; my ($key, $val) = split /=/; set_value_for_key( $hash, $key, $value );