Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Hash element access -dereferenced hashes

by reaper9187 (Scribe)
on Dec 06, 2012 at 10:00 UTC ( [id://1007506]=perlquestion: print w/replies, xml ) Need Help??

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

Hi everyone, I'm kinda new to perl and i was working with hashes for developing a particular application. Here goes my problem :
I have two hashes ,say (key1,value1) and (key2,value2)... What is the best possible way to associate key 1 with value 2. ( want to print them). value 1 is the key for hash 2.
I tried to to do it by using something like  $hash2{$hash1{$key1}} to try to extract the value but it returns a null character but works perfectly when you plug in the scalar value of value2 instead of $hash{$key1}.

Please help ..!! thank you..

Replies are listed 'Best First'.
Re: Hash element access -dereferenced hashes
by choroba (Cardinal) on Dec 06, 2012 at 10:10 UTC
    Works for me. Note I use key1 as a string, not the undefined variable $key1. Use strict and warnings to catch this kind of problems.
    %hash1 = (key1 => "value1"); %hash2 = (value1 => "Gotcha"); print $hash2{ $hash1{key1} }, "\n"; # Not $key1!
    Output:
    Gotcha
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Hash element access -dereferenced hashes
by tobyink (Canon) on Dec 06, 2012 at 10:13 UTC

    Is this the kind of thing you mean?

    my %hash1 = ( xxx => 'yyy' ); my %hash2 = ( yyy => 'zzz' ); print $hash2{$hash1{xxx}}, "\n"; # zzz
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Hash element access -dereferenced hashes
by reaper9187 (Scribe) on Dec 06, 2012 at 11:10 UTC
    Well this is embarassing ..!! i realized after writing the code that i had switched the order and the entry map{lucy} does not exist.... i suspect the problem is with the value 2 itself... update: as suspected , it was a null entry .. i tried changing the index and it works now ..thanks a lot..!!
Re: Hash element access -dereferenced hashes
by reaper9187 (Scribe) on Dec 06, 2012 at 10:35 UTC
    the above codes work just fine.. dunno whats wrongs with the exp .. i'll post the code .. c if u can figure out the problem
    i have two defined hashes cell and map,
    $a = "Lucy"; my %cell=> = ( Lucy => '12' ); my %map=> = ( 12 => '45' );

    i try printing it using the command
    print "$cell{$map{$a}}\n";
    but it throws the exception-- use warnings: - use of uninitialized value within %cell in string at line...
      syntax error at ./1.pl line 2, near "=> ="
      Please, post the real code.

      After fixing that, just switch the hashes. $map{Lucy} does not exist.

      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-03-19 07:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found