Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

by Util (Priest)
on Nov 24, 2011 at 15:21 UTC ( #939910=note: print w/replies, xml ) Need Help??


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

This will warn when run with or without `-c`:
sub no_dup_keys { die "Odd number of elements" if @_ % 2; my %h; while ( my ( $key, $new_val ) = splice @_, 0, 2 ) { if ( exists $h{$key} ) { warn "Duplicate key: $key, old_value: $h{$key}," . " new_value: $new_val\n "; next; # Remove for rolling overlay. } $h{$key} = $new_val; } return %h; } my %hash; BEGIN{ %hash = no_dup_keys( "one" => "1", "two" => "2", "one" => "3", ); }
  • 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
Node Status?
node history
Node Type: note [id://939910]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2020-06-02 18:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (19 votes). Check out past polls.

    Notices?