in reply to Re: quick question about hash element access
in thread quick question about hash element access

I suspect the question was not intended to be limited to hash paths of a specific depth. I'd suggest something like this:
sub hpath(\%$) { my ($href, $path) = @_; while ($path =~ /\G(.+?)\.?/gc) { $href = $href->{$1}; } return $href; } my %c = (1 => { 2 => { 3 => 4 }}); print hpath(%c, "1.2.3");
Only with error checking, of course. Substitute while  ... with foreach (split(/\./, $path) (and $1 with $_) for slightly different flavor.

Replies are listed 'Best First'.
Re: Re: Re: quick question about hash element access
by argggh (Monk) on Jun 28, 2003 at 15:27 UTC
    (For a another flavor altogether, try reduce:
    use List::Util qw(reduce); my %c = (1 => { 2 => { 3 => 4 }}); print reduce { $a->{$b} } \%c, split(/\./, "1.2.3");
    )