in reply to Re: setting hash keys by array
in thread setting hash keys by array

$node = \$$node->{$keys[$i++]} while $i < @keys;

++. IMHO, this is the only sane approach. But why not $node = \$$node->{$_} for @keys;?

Update
Reputation: 41, it says. MADNESS. I only suggested the use of for LIST instead of explicitly using an index variable. It's a style related comment. Why on earth did you people upvote this THIS MUCH? There are better nodes getting lower scores. Vote for those. ++ japhy, ++ demerphq for they did the work. I only complained about style.

- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.

Replies are listed 'Best First'.
Re: Re: Re: setting hash keys by array
by demerphq (Chancellor) on Sep 09, 2002 at 18:20 UTC
    Nice stuff to both you and japhy

    While trying to grok that code I stumbled on an interesting different approach... Its not suitable for building a tree from many lists, but is suitable for building a HOH out of a single list:

    use Data::Dumper; my @keys = qw( this that those ); my $node='value'; $node={$_=>$node} foreach reverse @keys; print Dumper $node; __END__ $VAR1 = { 'this' => { 'that' => { 'those' => 'value' } } };

    --- demerphq
    my friends call me, usually because I'm late....

Re: Re: Re: setting hash keys by array
by fruiture (Curate) on Sep 09, 2002 at 17:55 UTC

    Why not:

    my @chain = qw/a b c d/; my %hash; sub foo(\%@){ local $_ = shift; $_ = $_->{scalar shift} = {} while @_; } foo %hash => @chain;

    ? it's becoming obfuscated ...

    --
    http://fruiture.de
Re: Re: Re: setting hash keys by array
by japhy (Canon) on Sep 09, 2002 at 17:21 UTC
    My mistake -- I lifted it from old code of mine. Recent versions use your suggested alteration.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;