Dup-checking in Perl
From
Angus McLeod@VERT/ANJO to
All on Wednesday, September 12, 2007 12:43:00
Here's an interesting little chore!
Given a list of phrases, check for duplicates, and add a suffix to
distinguish the duplicates from each other. Example:
Pink Rainbow
Yellow Lizard
Slackware
Automotive Mayhem
Yellow Lizard -> Yellow Lizard 2
Magnifying Glass
Peppermint Candy
The second duplicate is given the suffix "2" to distinguish it from the
first occurrence of the phrase. Code is simple:
foreach $item (@list) {
if ( $dups{$item}++ ) {
printf "%-24s -> %s %d\n", $item, $item, $dups{$item};
} else {
printf "%-24s\n", $item;
}
}
But it will not do! If there is alredy a "Yellow Lizard 2" in the list, I
am changing one duplicate so that it duplicates another!
Pink Rainbow
Yellow Lizard
Slackware
Yellow Lizard 2
Automotive Mayhem
Yellow Lizard -> Yellow Lizard 2
Magnifying Glass
Peppermint Candy
Notice that the duplicate "Yellow Lizard" is renamed so that it no longer duplicates the first "Yellow Lizard", but it now duplicates the "Yellow
Lizard 2" that was in the list to start with. Ideally. "Yellow Lizard"
would become "Yellow Lizard 3".
Also, the duplicate of "Yellow Lizard" could appear *before* "Yellow
Lizard 2" in the list, so we will have to use a multi-pass approach in
order to learn about all names in advance, before we can decide how to
munge them if duplicates appear.
It's starting to get nasty!
---
Playing: "Keep yourself alive" by "Queen" from the "Queen" album.
þ Synchronet þ Programatically generated on The ANJO BBS