Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
go ahead... be a heretic
 
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 making s'mores by the fire in the courtyard of the Monastery: (16)
As of 2013-05-22 17:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best material for plates (tableware) is:









    Results (466 votes), past polls