EdwardG,
Here is the longer, but more elegant code I mentioned.
#!/usr/bin/perl use strict; use warnings; my %data = map { chomp; $_ => [ split ] } <DATA>; my $last = 0; for ( keys %data ) { $last = @{$data{$_}} if @{$data{$_}} > $last; $_ = ucfirst lc $_ for @{$data{$_}}; } for my $index ( 0 .. $last ) { for my $name ( map { $data{$_} } keys %data ) { next if ! $name->[$index]; for my $length ( 1 .. length $name->[$index] ) { if ( ! Match( $name, $index, $length ) ) { $name->[$index] = substr( $name->[$index], 0, $length +) ; last; } } } } sub Short { my ( $name, $index, $length ) = @_; return join " ", @{$name}[ grep $_ < $index, 0 .. $#$name ], substr( $name->[$index], 0, $length ), @{$name}[ grep $_ > $index, 0 .. $#$name]; } sub Match { my ( $name, $index, $length ) = @_; my $s_name = Short( $name, $index, $length ); for my $t_name ( map { $data{$_} } keys %data ) { next if @$name != @$t_name || $name eq $t_name; my $s_test = Short( $t_name, $index, $length ); return 1 if $s_name eq $s_test; } return 0; } print "@{ $data{$_} } => $_\n" for sort keys %data;
If you want it to be even faster, add the following test as the second || condition on line 36.
index($t_name->[$index], substr($name->[$index], 0, 1))
Cheers - L~R

In reply to Re: Re: Re: Re: Re: Re: Re: Generate unique initials from a list of names by Limbic~Region
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.