Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Getting the Value of a single hash key

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

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

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"; } } }

Replies are listed 'Best First'.
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:

    push( @{$hash{$key}}, $src);
    to:
    $hash{$key}=$src;

    And all should be well.

    Good Luck

(jeffa) Re: Getting the Value of a single hash key
by jeffa (Bishop) on May 06, 2003 at 19:35 UTC
    ($value, $key) = split(/=/, $line);
    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?

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (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.

    Cheers

      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

Log In?
Username:
Password:

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

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

    No recent polls found