Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: quick question about hash element access

by Nkuvu (Priest)
on Jun 27, 2003 at 21:12 UTC ( [id://269743]=note: print w/replies, xml ) Need Help??


in reply to quick question about hash element access

Something like the following?

#!/usr/bin/perl -w use strict; my %test_hash; $test_hash{'a'}{'b'}{'c'}{'d'} = "Yay"; print get_id("a.b.c.d"), "\n"; sub get_id { my @parts = split /\./, $_[0]; # Currently written to access a global hash return $test_hash{$parts[0]}{$parts[1]}{$parts[2]}{$parts[3]}; }

I'm sure other monks can find more efficient (or bulletproof) ways. But this seems to work -- you could probably easily alter it so that it accepts a reference to a particular hash and uses that inside the sub.

Also note that $hash{'a'}{'b'}{'c'} is the same as $hash{'a'}->{'b'}->{'c'}

Replies are listed 'Best First'.
Re: Re: quick question about hash element access
by argggh (Monk) on Jun 27, 2003 at 21:23 UTC
    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.
      (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");
      )
Re: Re: quick question about hash element access
by LanceDeeply (Chaplain) on Jun 27, 2003 at 21:28 UTC
    added to your code so it can take a variable length identifier, and accept the hashref.
    my %test_hash; $test_hash{'a'}{'b'}{'c'}{'d'} = "Yay"; print get_id(\%test_hash, "a.b.c.d"), "\n"; sub get_id { my $thingy = shift; my @keys = split /\./, shift; foreach my $key ( @keys ) { $thingy = $$thingy{$key}; } return $thingy; }

      Interesting. But I have a question (not being totally fluent in references).

      What exactly does the $thingy = $$thingy{$key} line do? My guess is that it assigns a scalar value referred to by the reference which is contained in $thingy{$key}. But I'm not even sure that sentence makes sense, much less if it's accurate. :)

        you are correct sir-
        we are assigning $thingy to the value of whatever is refered to by $thingy{'a'}, in this case, it's the next hashref. this assignment is just repeated a few times till we're all out of keys.

        what's bad about this is that it assumes the caller will pass in a valid path. i guess error checking will be left up to OP...
        $$thingy{$key} is equal to $thingy->{$key}, so it returns the value corresponding to the key $key in the hash pointed to by the reference $thingy.
Re: Re: quick question about hash element access
by tilly (Archbishop) on Jun 27, 2003 at 21:21 UTC
    I suspect that the key requirement is variable depth of nesting.

    (Your solution assumes a fixed depth.)

      Hmm, yes, I hadn't thought about that possible requirement. And here I thought I had an easy solution. :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://269743]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-26 08:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found