Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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

by Limbic~Region (Chancellor)
on Mar 12, 2004 at 17:43 UTC ( [id://336216]=note: print w/replies, xml ) Need Help??


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

EdwardG,
You are right - I only had a minimal chance to test it. I think it works to your satisfacation. Sorry it took a while to get back to you - stupid thing called work ;-)
#!/usr/bin/perl use strict; use warnings; my %name = map {chomp; $_ => undef} <DATA>; my $num_names = 0; for my $person ( keys %name ) { my @names = split " " , $person; $num_names = @names if @names > $num_names; $_ = ucfirst lc $_ for @names; $name{ $person } = \@names; } my $finished = 0; my $index = 0; while ( ! $finished ) { my $update = 0; for my $person ( keys %name ) { next if ! $name{$person}[$index]; if ( $name{$person}[$index] =~ /(\w+)\w/ ) { my $new_name = $1; if ( ! grep { $new_name eq $_ } map { $name{$_}[$index] } grep { @{$name{$_}} == @{$name{$person}} } keys %name ) { $update = 1; $name{$person}[$index] = $new_name; } } } $index++ if ! $update; $finished = 1 if $index > $num_names; } print "$_ => @{ $name{$_} }\n" for keys %name;
Cheers - L~R

Replies are listed 'Best First'.
Re: Re: Re: Re: Generate unique initials from a list of names
by EdwardG (Vicar) on Mar 12, 2004 at 20:33 UTC
    Alan Harper => A H <-- AMBIGUOUS! Adam Harper => Ad Ha

    The requirement is that all derived 'initials' should be unambiguous in which name they refer to.

    'A H' could refer to Alan or Adam.

    My code does this:

    Adam Harper => AdHa Alan Harper => AlHa
      EdwardG,
      Ok - I interpreted the requirement differently. Since there are only two possibilities and "Ad Ha" can only refer to 1, "A H" must refer to the other. I believe the following accomplishes what you want - and is actually a bit better than your solution (I think). If I am wrong - I give up.
      #!/usr/bin/perl use strict; use warnings; my %data; my $last = 0; while ( <DATA> ) { chomp; my @names = split; $last = @names if @names > $last; $_ = ucfirst lc $_ for @names; $data{ "@names" } = \@names; } for my $index ( 0 .. $last ) { for my $person ( keys %data ) { next if ! $data{$person}[$index]; my $old_name = $data{$person}[$index]; for my $length ( 1 .. length $old_name ) { $data{$person}[$index] = substr( $old_name, 0, $length ); my $match = 0; for ( keys %data ) { next if $_ eq $person || @{$data{$_}} != @{$data{$pers +on}}; my $test_name = $data{$_}[$index]; $data{$_}[$index] = substr( $test_name, 0, $length ); my ($s_name , $s_test) = ("@{$data{$person}}", "@{$dat +a{$_}}"); $data{$_}[$index] = $test_name; if ( $s_name eq $s_test ) { $match = 1; last; } } last if ! $match; } } } print "@{ $data{$_} } =>\n" for keys %data;
      The reason why I say my code is a bit better is because:
      __DATA__ Victor Mcduffie Viola Mcnamee # Yours Vic Mcd Vio Mcn # Mine V Mcn V Mcd

      Cheers - L~R

        Bingo, yours is now better than mine. :)

        ++

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-18 22:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found