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

Hi monks,

My problem is I have unique keys and am storing an array of values
for each. As each key appears in the file several times, new values just keep on
getting pushed in. At least, that's the idea. The problem is it won't stop
creating keys - my exists checker must be wrong :(

This is what Im trying:

if(exists($matrixhash{$key})){ print "Key exists ! \n"; <STDIN>; }else{ #it doesn't print "$key stored as a key here\n"; # ADD KEY AND VALUE TO HASH push(@{$matrixhash{$key}},$store); print "key is $key and value is @{$matrixhash{$key}}\n"; #<STDIN>; }
So I'm trying to say: if this key exists, don't create it again.
I am going to add something after "Key exists !" line to push more values into the array.

If it doesn't exist, create the key and push the value of $store into the array.

hope that makes some sense,
cheers,
basm101

Replies are listed 'Best First'.
Re: checking keys exist in 1 key:multiple value situation
by robartes (Priest) on Jan 30, 2003 at 11:29 UTC
    When I run your code with the following values:
    my $key="test"; my %matrixhash=('test'=>'camel'); my $store="store me";
    it correctly reponds with "Key exists ! ". Your code as given is correct. Are you sure that $key contains what you expect it to contain?

    Perhaps if you post some more code, the problem might become more apparent.

    CU
    Robartes-

Re: checking keys exist in 1 key:multiple value situation
by PodMaster (Abbot) on Jan 30, 2003 at 11:29 UTC
    The problem is with your data. What you might think are duplicate keys aren't really (the code which you showed would not do what you say, so $key must not be what you think it should be).

    use Data::Dumper and print Dumper\%matrixhash to see what's actually in there. I'm guessing you have stray whitespace.


    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    ** The Third rule of perl club is a statement of fact: pod is sexy.

      hmm..maybe it's a different problem then. I used Data::Dumper like suggested
      and it printed:

      $VAR1 = { 'NERME' => 1 }; where NERME is a key and the first value I shove into its array is 1.
      Is this typical Data::Dumper "show me the hash" output ? It's the first
      time I've tried using Data::Dumper.
      Thanks,

      basm101
        Yes, that is typical Data::Dumper output. Example
        #!/usr/bin/perl use Data::Dumper; use strict; use warnings; my %hashes = ( 1..4); push @{$hashes{z}}, 1..3; die Dumper\%hashes; __END__ $VAR1 = { '1' => 2, '3' => 4, 'z' => [ 1, 2, 3 ] };
        Please read the site how to and the perl monks faq, and format your post correctly (use <CODE></CODE> tags for code)


        MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
        ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: checking keys exist in 1 key:multiple value situation
by Gilimanjaro (Hermit) on Jan 30, 2003 at 13:50 UTC
    You don't need to check!

    Perl uses auto-vivication to create your anonymous array as the hash value if it didn't exist yet...

    You'll still need to make sure your key is correct though.