Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Throw compilation error(or warning) if duplicate keys are present in hash

by TJPride (Pilgrim)
on Nov 24, 2011 at 08:47 UTC ( [id://939823]=note: print w/replies, xml ) Need Help??


in reply to Throw compilation error(or warning) if duplicate keys are present in hash

Thinking outside the box - is it possible to modify whatever is generating this .pm (I'm assuming it's generated, otherwise you could easily fix the problem...) to check for duplicate keys up front and eliminate them or deal with them somehow? Seems overly complex to mess with them on the far end.

Replies are listed 'Best First'.
Re^2: Throw compilation error(or warning) if duplicate keys are present in hash
by I_love_perl (Novice) on Nov 24, 2011 at 09:28 UTC

    Hi TJPride,

    Thanks for your response

    It is a manually generated .pm file by the people who are not very much familiar with perl syntax. Hence I suspect that there might be a good chance of mistyping an existing key.

    In this case we are not looking for eliminating the duplicated hash keys, rather we are looking for updating .pm file properly with a new key for the duplicated one's.

    So we need to know if some key is duplicated

    As I know writing a small validating perl script will do this , but I was thinking if there is any approach available in perl by default or addon.

      It is a manually generated .pm file by the people who are not very much familiar with perl syntax. Hence I suspect that there might be a good chance of mistyping an existing key.
      You could get those people into assigning not to %hash, but to @hash, i.e. an array instead. Later in the program, you check @hash whether a "key" occurs twice, before assigning to %hash.

      -- 
      Ronald Fischer <ynnor@mm.st>

        Hi Ronald

        Yes. Absolutely we can write a piece of code to check for the existence of duplicate keys in a hash or array. But I felt like it should be present in some perl program check utilities (like use strict, use warnings).

        As I don't know if this facility is available in perl I posted this query to know if there some package(or pragma) available for this kind of checks.

      To implement rovf's suggestion:

      use Data::Dumper; use strict; use warnings; my %hash; assignWithDupes([ "one" => "1", "two" => "2", "one" => "3", "three" => "4", "two" => "5", "one" => "6", ], \%hash); print Dumper(\%hash); sub assignWithDupes { my ($arr, $hash) = @_; my ($i, $j, $key, $val); for ($i = 0; $i < $#$arr; $i += 2) { $key = $arr->[$i]; $val = $arr->[$i+1]; if (!defined $hash->{$key}) { $hash->{$key} = $val; next; } for ($j = 2; defined $hash->{"${key}_$j"}; $j++) {} $hash->{"${key}_$j"} = $val; } }

      Output (modified to put it in order):

      $VAR1 = { 'one' => '1', 'one_2' => '3', 'one_3' => '6' 'two' => '2', 'two_2' => '5', 'three' => '4', };

      As you can see, successive instances of the same key are assigned to _2, _3, etc.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-03-29 11:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found