Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Re: Generate unique initials from a list of names

by EdwardG (Vicar)
on Mar 13, 2004 at 10:39 UTC ( [id://336358]=note: print w/replies, xml ) Need Help??


in reply to Re: Generate unique initials from a list of names
in thread Generate unique initials from a list of names

Your code certainly looks nicer than my attempt, nicer even than L~R's attempt, but it doesn't work in all cases.

__DATA__ A Remus A Ripper A Robertson A Robinson A Rupee d:\tmp>336330.pl ARe A Remus ARi A Ripper ARe A Robertson ARi A Robinson

...some duplicates, and what's happened to Rupee?

Replies are listed 'Best First'.
Re: Re: Re: Generate unique initials from a list of names
by BrowserUk (Patriarch) on Mar 13, 2004 at 11:30 UTC

    This fixes both those bugs, but there may be other edge cases.

    The fudge wasn't necessary once I fixed the real problem:) Yes. 'ZZZZZZZZZZZ' is a fudge, but I am re-using mapNM which is a utility routine that wasn't specifically design for this case and this is the 'cheap fix' to flush the pump.

    #! perl -slw use strict; sub mapNM (&@) { my $code = shift; map{ local( *a, *b ) = \( @_[ 0, 1 ] ); $code->(shift); # } 0 .. @_ - 2 } 0 .. @_ - 1 } sub strcmp{ my( $p, $b ) = ( $_[2]||0, 0 ); $p++ until $b = substr( $_[ 0 ], $p, 1 ) cmp substr( $_[ 1 ], $p, 1 ); $p * $b; }; my @names = sort map{ join' ', map{ $_ = ucfirst } split '[^a-z0-9]+', + lc } <DATA>; my %abbrev; print "@$_" for mapNM{ if( defined $b ) { my $n = 0; while( exists $abbrev{ $a->[ 0 ] } or $a->[ 0 ] eq $b->[ 0 ] ) { $n = strcmp $a->[ 1 ], $b->[ 1 ], abs($n)+1; if( $n ) { $a->[ 0 ] .= substr( $a->[ 1 ], abs $n, 1 ); $b->[ 0 ] .= substr( $b->[ 1 ], abs $n, 1 ); } } } $abbrev{ $a->[ 0 ] } = undef; $a } sort{ $a->[ 0 ] cmp $b->[ 0 ] } map{ [ join('', m[([A-Z])]g), $_ ] #} @names, 'ZZZZZZZZZZZZ'; } @names;

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://336358]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-24 22:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found