Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Direct reference to any element in a hash-of-hash

by jaa (Friar)
on Jun 15, 2006 at 12:12 UTC ( [id://555478]=perlquestion: print w/replies, xml ) Need Help??

jaa has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I have a Hash-of-hash, and each of the child-hashes contains a value that I am interested in accessing just once. This value is the same for all of the child hashes.

I have found two ways to access the value, but wonder if there is a better way??

my $hoh = { apples => { source => 'uk', colour => 'red', }, pears => { source => 'uk', colour => 'green', }, bananas => { source => 'uk', colour => 'yellow', }, }; # naff: get source, using var in a list assignment my ( $any ) = values %$hoh; print "source: ", $any->{source}, "\n"; # naff: create an anonymouse array and access element[0] print "source: ", ${[values %$hoh]}[0]{source}, "\n";

Regards,

Jeff

Replies are listed 'Best First'.
Re: Direct reference to any element in a hash-of-hash
by neniro (Priest) on Jun 15, 2006 at 12:45 UTC
    If you don't want to get all those values or keys, you could use each to get just the first pair:
    #!/usr/bin/perl use strict; use warnings; my $hoh = { apples => { source => 'uk', colour => 'red', }, pears => { source => 'uk', colour => 'green', }, bananas => { source => 'uk', colour => 'yellow', }, }; my (undef, $v) = each %$hoh; print $v->{'source'}, "\n";
    If you want to use each later in your script, remember to reset the iterator.
Re: Direct reference to any element in a hash-of-hash
by ioannis (Abbot) on Jun 15, 2006 at 12:46 UTC
    When you access a HoH this way, a random child-hash is printed; the is no guaranty it will be the first child-hash because the hash has no concept of element ordering. You will receive a random element.
    # All these are random accesses. my ( $any ) = values %$hoh; print "source: ", ${[values %$hoh]}[0]{source};
Re: Direct reference to any element in a hash-of-hash
by Moron (Curate) on Jun 15, 2006 at 12:44 UTC
    The term 'better' will mean different things to different people in different contexts. The following derives some extra speed from the assumption that there is only one value for source throughout the hash:
    print "source: ", $hoh -> { $_ }{ source } . "\n" and last for keys %$ +hoh;

    -M

    Free your mind

Re: Direct reference to any element in a hash-of-hash
by jaa (Friar) on Jun 15, 2006 at 12:12 UTC
    PS - I do know that bananas don't really come from the uk!

    Regards,

    Jeff

Log In?
Username:
Password:

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

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

    No recent polls found