in reply to Nesting... or something.
I used @_ instead of your $i reference, but change things around if you really need it that way. The idea's the same. Perhaps there's a more elegant solution but I couldn't see one at the moment. Have fun.# Note: Don't prototype () since that won't let you pass args sub builder { my $data = shift; # $i in your code I'm keep +ing in @_ my %hash = (); my $ptr = \%hash; # Set up a pointer to %has +h for (my $cur = 0; $cur < $#_; $cur++) { $ptr->{$_[$cur]} = {}; # Manipulate using our poi +nter $ptr = $ptr->{$_[$cur]}; # and advance it as we go } $ptr->{$_[$#_]} = $data; # Do our last hash return %hash; # and do what you want wit +h %hash } my %hash = builder('abcde', 'a', 'b', 3); print $hash{'a'}{'b'}{3}; # 'abcde'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: RE: Nesting... or something.
by jettero (Monsignor) on Jun 03, 2000 at 21:16 UTC | |
by mdillon (Priest) on Jun 03, 2000 at 22:16 UTC | |
by jettero (Monsignor) on Jun 05, 2000 at 09:52 UTC | |
by Fastolfe (Vicar) on Jun 05, 2000 at 00:01 UTC |