This code is short, though it loops a bit, and it produces unique, logical (though not necessarily obvious), short abbreviations.

#! perl -slw use strict; sub mapNM (&@) { my $code = shift; map{ local( *a, *b ) = \( @_[ 0, 1 ] ); $code->(shift); } 0 .. @_ - 2 } sub strcmp{ my( $p, $b ) = (0) x 2; $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>; print "@$_" for mapNM{ if( $a->[ 0 ] eq $b->[ 0 ] ) { my $n = strcmp $a->[ 1 ], $b->[ 1 ]; if( $n ) { $a->[ 0 ] .= substr( $a->[ 1 ], abs $n, 1 ); $b->[ 0 ] .= substr( $b->[ 1 ], abs $n, 1 ); } } $a; } sort{ $a->[ 0 ] cmp $b->[ 0 ] } map{ [ join('', m[([A-Z])]g), $_ ] } @names; __DATA__ A Robertson A Robinson A A Milne A A Milner [SNIP]

Output

P:\test>336125.pl AAM A A Milne AAMr A A Milner AB Amanda Basile AC Alvaro Cangemi AD Annabelle Ducker AGl Allan Goris AGn Anthony Granado AHd Adam Harper AHs Ashlee Hurtt AK April Kain AM Anthony Mcdevitt AO Albert Osterman AP Alan Pfaff ARe A Robertson ARi A Robinson ARl Allan Razor ARm Alma Ratcliffe AR Arthur Ragsdale ARS Alphonse Romeo Smith ASa Alana Stutsman ASi Alison Sands ASn Antonio Starnes ASr Arlene Stephen AS Armand Stower AV Alan Vanwinkle Ba Ballentine Bi Binns B Brittian BAB Big Al Bundy BD Brandon Dominick BE Bobby Eaton BN Brittanie Noviello BP Blanche Page BSe Bertha Schuck BSi Billy Sundberg BTe Bernice Turnage BTu Bulah Twombley BW Bobby Winkelman BZ Beata Zar CB Carey Balck CCa Candace Cote CCh Chris Creekmore CD Clinton Dahmen CE Clayton Elbert CGa Carey Grishaber CGh Chad Guarino CG Clinton Ganley CL Cheryl Langley CMa Clare Milum CMi Clinton Mcnaught CP Chandra Puffer CR Cody Ridout CT Carey Twedell CV Cody Vigo Di Dilbert Do Doggett DA Doris Adkins DB Darryl Buchta DC Daniel Colunga DHl Dale Hernandes DHn Dane Hasychak DS Debbie Schlueter DT Darren Tacey DV Darren Vanaman EB Eileen Bard EC Ernest Cowley ED Erik Duley EF Ernest Frey EG Eve Goodfellow EHi Elizabeth Hendricks EHn Elnora Hornbuckle EHi Ericka Hodgin EHn Ernest Helmick EK Esmeralda Kardos ER Elinor Rita ES Elwood Steketee EV Ewa Villerreal FJ Fidel Jacoway FL Florence Lillie FM Felicia Manson FO Fernando Outler FPi Fidel Paone FPr Fred Pyles G Guinn GGe Gerald Graziano GGu Guy Gatton GL George Lamoureux GM Gerald Minnich GN Gina Nickelson GR Gladys Rothman GT Genna Tranbarger GWl Glenda Wheat GWr Gregory Walls H Heinricher HC Hugh Charland HDa Harriet Dreyer HDu Hugh Dipalma HF Hugh Fuselier HH Harold Houser HM Hugh Minaya HQ Hannah Quijano HS Howard Sirois IM Inez Marion IN Ida Noe IPI I Palindrome I JA James Angell JBa Jamie Brickhouse JBo Joyce Brian JC James Cravens JGa Jami Gula JGe Jerri Giesen JG Ji Greenier JHa Jamie Huot JHe Jerri Houseknecht JMe Jeannette Macaluso JMo Jonathan Matos JN Jerri Nickson JO Jessie Olah JOB Jimmy O Brien JOL James O Leary JSo John Smith JSu Julio Sepeda JW Jason Willson K9 K9 Ki Kingsbury K Koteles KBl Keila Brue KBt Keith Berner KB Kevin Batchelder KCe Kenneth Caraballo KCi Kimberly Cecil KD Kathryn Diaz KHa Katherine Holzer KHe Kelly Henegar KH Kimberly Hare KK Kristina Kasper KL Katie Lloyd KPa Karina Palka KPe Kevin Poindexter LBa Larita Battisti LBo Louise Belanger LC Laurie Carranza LF Lilia Fouse LG Louisa Galyean LKi Linda Kavanaugh LKy Lynda Knudsen LLr Lorrie Lobdell LLu Louis Leggett LM Laura Mcfadden LP Linda Power LSn Lonnie Sherrow LSr Lori Stjohn LS Louisa Swingle LT Lakisha Trees LW Louis Watterson MAc Marcel Avers MAg Margaret Amey MBe Mae Brantley MBl Mallory Brodbeck MB Maricela Bautch MCa Marcel Cuffia MCi Mildred Colburn MF Maggie Fernandez MGa Marlin Gryniuk MGe Merrilee Garelick MH Mark Haynie MK Martin King MMg Maggie Madore MMr Marcel Merow MMa Mathew Mayton MMe Medhi Majesh MMc Mickey Mouse MMn Minnie Mouse MN Ming Negro MR Madelene Robasciotti MSl Mallory Sickels MSr Marilyn Shumpert MS Max Sprau MT Manuel Trotter NH Neil Helbert NL Nicholas Lowell NP Nelson Pinzon NR Nelson Rayo NS Nathan Shuster NTi Nicole Tillman NTo Norman Thatcher OB Olivia Bridgeman PA Peter Agee PK Priscilla Katz PL Peggy Loggins PM Penelope Maize PV Peter Valadez R Royce RDa Raphael Dumaine RDo Robyn Duvall RF Randy Frederickson RG Roger Goldman RK Roy Kushner RL Ronald Looney RM Rachel Marchand RN Raymond Nava RS Rod Schoneman Sc Schrock Sy Sydow SA Shawn Ault SC Stanley Cleaver SO Scott Obryan SS Sylvia Salazar SW Sylvia Walther T Tompkins TB Tarra Bellantuono TCh Thomas Coleman TCy Tyrone Crossett TDi Tia Drakeford TDo Tom Demarco TD Tom Denada TGa Tabatha Goodsell TGy Tyrone Golub TH Tony Haight TP Tina Palomo TT Tiny Tim TW Tari Windish VMc Victor Mcduffie VMo Viola Mcnamee VN Vincent Nowak WM Wayne Maye WU Wayne Ulrich

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

In reply to Re: Generate unique initials from a list of names by BrowserUk
in thread Generate unique initials from a list of names by EdwardG

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.