in reply to updating file
Result:my @color = qw(red blue black); use Tie::IxHash; tie my(%color), Tie::IxHash; # Existing colors: foreach(@color) { $color{$_}++; } # Attempt to add some: foreach(qw(purple blue green red)) { $color{$_}++ or print "Ooh, that's a new one: $_\n"; } print "I've got:\n"; print " * $_\n" foreach keys %color;
Ooh, that's a new one: purple Ooh, that's a new one: green I've got: * red * blue * black * purple * greenAs you can see, it's easy to spot new additions, dupes are prevented, plus the original order of addition is preserved.
If this is for a file, you can just append the new color addition to the end of the file, when you encounter a new one. You then don't even need to use Tie::IxHash — a plain hash will do.
|
|---|