Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Using a hash key value as the key in another hash.

by c (Hermit)
on Aug 26, 2001 at 17:41 UTC ( [id://107929]=perlmeditation: print w/replies, xml ) Need Help??

The following code works:

#!/usr/bin/perl -wT use strict; my %first; my %second; my %third; $first{charles} = "success!"; $second{gina} = "charles"; $third{scott} = "gina"; print "$first{$second{gina}}\n";

but would it really be considered good form? i know that tmtowtdi is our mantra, but i think everyone would agree that just because it works doesnt necessarily mean that there arent better ways to do it.

humbly -c

Replies are listed 'Best First'.
Re: Using a hash key value as the key in another hash.
by chipmunk (Parson) on Aug 26, 2001 at 17:57 UTC
    I don't see any reason not to store a hash key as a value in another hash. Sometimes it's even useful to have two hashes that are opposites of each other, so you can look up a key in the first hash and get its value, or look up a value in the second hash and get its key.
Re: Using a hash key value as the key in another hash.
by jryan (Vicar) on Aug 26, 2001 at 19:03 UTC

    Of course, by the time you get to stuff like this:

    $self->{base}->{$self->{elements}->{$element}->{type}}

    It just gets plain hard to read. Other than that, theres nothing wrong with accessing a hash with a key from another at all.

Re: Using a hash key value as the key in another hash.
by rrwo (Friar) on Aug 26, 2001 at 21:16 UTC

    There's nothing wrong with it. Though if you start getting beyond two hashes to three or more, your code may start getting complex and confusing. You may want to consider using objects or functions to wrap around your lookups.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-03-29 13:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found