Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Getting the Value of a single hash key

by Anonymous Monk
on May 06, 2003 at 19:29 UTC ( #256003=perlquestion: print w/ replies, xml ) Need Help??

Hello Monks,

I'm trying to finish a simple script that first builds a key/value hash. I will then query the hash to test the equality of a value with a new given value.

I don't understand why this doesn't work:

if(($hash{$key}) eq $value)

Any ideas?

while ($line = <F>) { chomp($line); ($value, $key) = split(/=/, $line); if(exists $hash{$key}) { if(($hash{$key}) eq $value) { print "YES: ", ($hash{$key}) ," == $value\n"; } else { print "NOT: ", ($hash{$key}) ," != $value\n"; } } }

Comment on Getting the Value of a single hash key
Download Code
(jeffa) Re: Getting the Value of a single hash key
by jeffa (Chancellor) on May 06, 2003 at 19:35 UTC
    Since the key is usually on the left side of the equal sign, shouldn't that be:
    ($key, $value) = split(/=/, $line);
    I changed that line and your code worked for my test case.

    UPDATE: regarding your reply below, please show us what your data looks like. That is, what are the contents of the F filehandle?


    (the triplet paradiddle with high-hat)
      I know, but the file I have is formatted value=key. So that is correct. The problem is that I get this output:
      NOT: ARRAY(0x1a99a98) != Source1 NOT: ARRAY(0x1a752ec) != Source2 NOT: ARRAY(0x1a99a98) != Source3
        You may be building your hash incorrectly, because it looks like the values are array references. Could you post that code?
Re: Getting the Value of a single hash key
by pzbagel (Chaplain) on May 06, 2003 at 19:57 UTC

    That ouput you gave us is enlightening. It suggests that when you are putting the %hash hash together, you are inserting array references instead of the values you are expecting. I suspect that in the code that is inserting the key/value pairs into %hash, you need to dereference the value. If you can't find the error, post the portion of your code that assigns the key/value pairs to %hash and we can help you.


      Here is the code:
      my $path; my %hash = (); #created items/objects my $line; while ($line = <CREATEFILE>) { chomp($line); ($src, $key) = split (/=/, $line); push( @{$hash{$key}}, $src); }
        Here is an example of the data. The second file handle does not have unique keys so I can't build a hash to test with it.

        There are two source files:

        #Listings Filehandle <CREATEFILE> Source1=Times Source2=DJNews Source3=Yahoo #Repeat Stocks Filehandle <F> F=Source1 F=Source3 LNUX=Source2 AMZN=Source1 INTL=Source3 INTL=Source1
Re: Getting the Value of a single hash key
by pzbagel (Chaplain) on May 06, 2003 at 20:21 UTC

    Ahh, you don't need to use push, you can change the line:


    And all should be well.

    Good Luck

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://256003]
Approved by Paladin
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2014-12-29 02:20 GMT
Find Nodes?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?

    Results (184 votes), past polls