Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

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 ( [id://939826]=note: print w/replies, xml ) Need Help??


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

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.

  • Comment on Re^2: Throw compilation error(or warning) if duplicate keys are present in hash

Replies are listed 'Best First'.
Re^3: Throw compilation error(or warning) if duplicate keys are present in hash
by rovf (Priest) on Nov 24, 2011 at 10:02 UTC
    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.

Re^3: Throw compilation error(or warning) if duplicate keys are present in hash
by TJPride (Pilgrim) on Nov 24, 2011 at 14:06 UTC
    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://939826]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-26 00:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found