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

Hello Monks.
I use $fname variable to identify/locate a key within
a hash which is a dbm file(simple database).
When I locate this key in a hash-I unpack it first with
a command:
($fname,$lname,$age,$dep)= unpack ($pack_rules,$DATABASE{$fname});
then after modifying something (except $fname of course)
I pack it back with a command:
$DATABASE{$fname}=pack ($pack_rules,$fname,$lname,$age,$dep)
The problem is that when I look for a key existence in a
hash-It gives me 2 keys with the same name but with
different values...
I'd like to have a single key after this operation.
Any suggestions?
Thanks in advance.

Replies are listed 'Best First'.
Re: Two keys with the same name in a hash.
by Random_Walk (Prior) on Feb 09, 2005 at 10:33 UTC
    Hi sashac88,

    You are possibly messing with your $fname when you do ($fname,$lname,$age,$dep)= unpack ($pack_rules,$DATABASE{$fname}); and writing back to a different key. Why do you store they key with the data stored under they key ? Do you original derive the key in this way from the data ?

    When you say "The problem is that when I look for a key existence in a hash-It gives me 2 keys" you are not making much Perl sense. To check for the existence of a key one usually1 does exists($hash{$key}) This can only return true/false, it returns no keys, not one and certainly not two :)

    what do you see if you put in a print join ", " keys %DATABASE; ? Bet you don't see duplicate keys there so what are you doing to make you think you have duplicate keys ? An example of the code that you use to "look for key existence" would help a lot.

    Cheers,
    R.

    1. I say usually because although I know no other way to check key existence I am not certain there is no other cunning trick known to the brethren

    Pereant, qui ante nos nostra dixerunt!
      $var=join "," ,keys %DATABASE;
      print "$var\n";
      that gave me:
      Alexander,Dima,Ofir,Udi,Moti,Dima
        I've found the problem.
        thanks for the help.
        Since I read the variables from Entry widgets
        It assigns different key in that its' like the previous +
        empty space.
Re: Two keys with the same name in a hash.
by PodMaster (Abbot) on Feb 09, 2005 at 09:33 UTC
    a hash which is a dbm file(simple database).
    Which one (there are many)?
    then after modifying something (except $fname of course)
    But the first thing you do is modify $fname (($fname,$lname,$age,$dep)= unpack ($pack_rules,$DATABASE{$fname});).
    Any suggestions?
    Give How (Not) To Ask A Question a read.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      I use dbmopen to create/open this dbm database file.
      The version of perl is 5.8.6 Activeperl.
      The key name and the first variable to pack- have the same
      name.
      Moreover, the problem doesn't grow further,i.e. When there
      already are 2 keys with the same name the last one can be
      modified with the mentioned above way.
        I use dbmopen to create/open this dbm database file.
        The version of perl is 5.8.6 Activeperl.
        You're probably using DB_File. Run
        perl -MAnyDBM_File -le"print for @AnyDBM_File::ISA"
        to verify.

        DB_File (or specifically DB_HASH, which is what you're using) doesn't allow multi-valued keys, so chances are you've misdiagnosed the situation. Insert the following snippet and examine your database

        use Data::Dumper; $Data::Dumper::Indent=1; $Data::Dumper::Useqq=1; die Dumper( \%DATABASE );
        you'll be suprised what you find.

        Unless you have a good reason to use dbmopen, you should switch to the tie interface.

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Two keys with the same name in a hash.
by jbrugger (Parson) on Feb 09, 2005 at 09:34 UTC

    What environment? Are you using strict? are you using global, lexical and local varables? to ONE hash ONE key has ONE name. A key can not exist twice (with the same name) in one hash

    It seems there might be a problem like in mod-perl, where variables aren't cleaned up properly, or that you're in a sub with a redefined value to me.
Re: Two keys with the same name in a hash.
by cog (Parson) on Feb 09, 2005 at 09:31 UTC
    How are you looking for that key?
      $var=join "," ,keys %DATABASE;
      print "$var\n";
      I see duplicates.
        You might be seeing things that *look* the same, but I'm pretty sure they aren't :-)

        Try this:

        use Data::Dumper; print Dumper([keys %DATABASE]);

        Does it have duplicate values inside? If so, tell us what those values are.

Re: Two keys with the same name in a hash.
by blazar (Canon) on Feb 09, 2005 at 09:57 UTC
    The problem is that when I look for a key existence in a hash-It gives me 2 keys with the same name but with different values...
    As you wrote it, whis is pure nonsense. Can you post a minimal but still working example exhibiting (what that you think to be) the problem?