A "common" practice for handling duplicate names in a database is to append non-printable characters after the name, in the order of insertion.
What you need is an invisible letter in Unicode. Just such a letter was proposed several years ago by typographer Michael Everson. His proposed name for the character was INVISIBLE LETTER. Unfortunately, the Unicode Consortium rejected his proposal. See Proposal to add INVISIBLE LETTER to the UCS and Every character has a story #11: U+???? (The Invisible Letter)
If there were such an invisible Unicode character, you could do something like this:
#!perl
use strict;
use warnings;
use open qw( :std :encoding(UTF-8) );
use charnames qw( :full );
use Unicode::Collate;
my $DISAMBIGUATOR_CHARACTER = "\N{LATIN SMALL LIGATURE FFL}"; # U+FB04
my %president_number_by; # President number by president name
my %seen;
while (<DATA>) {
chomp;
my ($name, $number) = split m/,/, $_, 2;
$seen{$name} = exists $seen{$name}
? $seen{$name} . $DISAMBIGUATOR_CHARACTER
: $name
;
$president_number_by{$seen{$name}} = $number;
}
my $collator = Unicode::Collate->new();
for my $name ($collator->sort(keys %president_number_by)) {
my $number = $president_number_by{$name};
$name =~ s/$DISAMBIGUATOR_CHARACTER+$//;
print "$name,$number\n";
}
exit 0;
__DATA__
Washington,1
Adams,2
Jefferson,3
Madison,4
Monroe,5
Adams,6
Jackson,7
Van Buren,8
Harrison,9
Tyler,10
Polk,11
Taylor,12
Fillmore,13
Pierce,14
Buchanan,15
Lincoln,16
Johnson,17
Simpson,18
Hayes,19
Garfield,20
Arthur,21
Cleveland,22
Harrison,23
Cleveland,24
McKinley,25
Roosevelt,26
Taft,27
Wilson,28
Harding,29
Coolidge,30
Hoover,31
Roosevelt,32
Truman,33
Eisenhower,34
Kennedy,35
Johnson,36
Nixon,37
Ford,38
Carter,39
Reagan,40
Bush,41
Clinton,42
Bush,43
Obama,44
Bush,45
This script produces this output:
Adams,2
Adams,6
Arthur,21
Buchanan,15
Bush,41
Bush,43
Bush,45
Carter,39
Cleveland,22
Cleveland,24
Clinton,42
Coolidge,30
Eisenhower,34
Fillmore,13
Ford,38
Garfield,20
Harding,29
Harrison,9
Harrison,23
Hayes,19
Hoover,31
Jackson,7
Jefferson,3
Johnson,17
Johnson,36
Kennedy,35
Lincoln,16
Madison,4
McKinley,25
Monroe,5
Nixon,37
Obama,44
Pierce,14
Polk,11
Reagan,40
Roosevelt,26
Roosevelt,32
Simpson,18
Taft,27
Taylor,12
Truman,33
Tyler,10
Van Buren,8
Washington,1
Wilson,28
(For the purpose of demonstrating more than two presidents with the same last name, I had to assume Barack Obama is re-elected in 2012 and Jeb Bush is elected in 2016. I'm sorry if this prospect offends you.)
This is a pure Unicode solution to the problem. There's no commingling of Unicode characters or graphemes with binary data. Unfortunately, however, there isn't a Unicode character with the general property L (Letter) that's guaranteed to be invisible. If there were, it would be just the right character to use for this "weirdo" purpose.
Why did I use the Unicode character LATIN SMALL LIGATURE FFL in the demo script? I don't know exactly. Maybe because it's a character that collates high and seems impossibly unlikely ever to occur in real data.
Jim
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.