Been there, done that. And found you need a bit more than you've got. ;-)
What you're doing works fine for a maximum of two values for a given key. But you need to check if the value already is an array ref:
if (exists $hash{$key}) {
if (ref $hash{$key} and ref $hash{$key} eq 'ARRAY') {
push @{$hash{$key}}, $new_val;
}
else {
my @letters_array = $hash{$key};
push @letters_array, $letters;
my $new_val = [@letters_array];
$hash{$key} = $new_val;
}
}
else {
$hash{$key} = $letters;
}
Of course, this becomes a problem if an ARRAY is a legitimate value to be stored in the array, as it is in your example. Now you have to decide what an array means here: does it mean that someone has pre-populated the value with some stuff, and may be adding one or more keys to the list?
my @pushed = (ref $letters && ref $letters eq 'ARRAY') ? @$letters :
+ $letters;
push @{$hash{$key}}, @pushed;
Or is each array ref a legitimate independant value? In this case, it's probably easiest if you treat all keys as always collided. This means that you always use an array. Then you can replace the whole if clause with:
push @{$hash{$key}}, $letters;
And then to check for actual collisions, you can do this:
for $key (keys %found_mod) {
if (@{$found_mod{$key}} > 1) {
print "This key -- $key -- has more than one value!\n";
}
}
As you can see, I've been down this road before. One place you see this type of problem (or at least, one place I've seen it ;-}) is in XML-like hierarchical data where elements can be repeated. For example, XML, HTML, etc. |