Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

by ricDeez (Scribe)
on Nov 24, 2011 at 08:27 UTC ( [id://939821]=note: print w/replies, xml ) Need Help??


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

The problem is that your %hash is valid as Perl will just store the last value for key "one". Perhaps this would work with Hash::MultiValue? I am thinking of something like this:

use strict; use warnings; use Hash::MultiValue; use 5.012; my $mhash = Hash::MultiValue->new( "one" => "3", "two" => "2", "two" => "1" ); die "Duplicate key detected for \%hash" if grep { my @a = $mhash->get_ +all($_); @a > 1 } $mhash->keys; print "We're good!";

I have not used it in anger myself but it would seem as though it would work for what you want!

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-18 02:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found