in reply to Throw compilation error(or warning) if duplicate keys are present in hash
If you want checking like that, then the way to do it is to read the data like it came from an external file, build the hash and report errors as the data is processed.
There are many formulations of how to do this. I present one below.
#!/usr/bin/perl -w use strict; use Data::Dumper; my $hash_data = <<END; #this is a "here is" string one 1 two 2 one 3 END my %seen; my %hash = map{ my ($text, $number) = split; warn "\"$text\" has been re-defined!\n" if $seen{$text}++; ($text,$number); }split (/\n/, $hash_data); print Dumper \%hash; __END__ Prints: "one" has been re-defined! $VAR1 = { 'one' => '3', 'two' => '2' };
|
|---|
| 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 11:53 UTC |