Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I parse people's full names into user names in Perl?

I need to convert a name in the format Parisi, Kenneth into the format kparisi.

Does anyone know how to do this in Perl?

Here is some sample data that is abnormal:

Zelleb, Charles F.,,IV
Eilt, John,, IV
Wods, Charles R.,,III
Welkt, Craig P.,,Jr.

These specific names should end up as czelleb, jeilt, cwoods, cwelkt, etc.


I have one more condition that is ruining my name builder

O'Neil, Paul

so far, Vinko Vrsalovic's answer is working the best when weird/corrupt names are in the mix, but this example above would come out as "pneil"... id be damned below judas if i cant get that o between the p and the n

like image 581
CheeseConQueso Avatar asked Dec 01 '22 12:12

CheeseConQueso


2 Answers

vinko@parrot:~$ cat genlogname.pl
use strict;
use warnings;

my @list;
push @list, "Zelleb, Charles F.,,IV";
push @list, "Eilt, John,, IV";
push @list, "Woods, Charles R.,,III";
push @list, "Welkt, Craig P.,,Jr.";

for my $name (@list) {
        print gen_logname($name)."\n";
}

sub gen_logname {
        my $n = shift;
        #Filter out unneeded characters
        $n =~ s/['-]//g;
        #This regex will grab the lastname a comma, optionally a space (the 
        #optional space is my addition) and the first char of the name, 
        #which seems to satisfy your condition
        $n =~ m/(\w+), ?(.)/;
        return lc($2.$1);
}
vinko@parrot:~$ perl genlogname.pl
czelleb
jeilt
cwoods
cwelkt
like image 92
Vinko Vrsalovic Avatar answered Dec 06 '22 10:12

Vinko Vrsalovic


I would start by filtering the abnormal data so you only have regular names. Then something like this should do the trick

$t = "Parisi, Kenneth";
$t =~ s/(.+),\s*(.).*/\l$2\l$1/;
like image 24
Brian Rasmussen Avatar answered Dec 06 '22 11:12

Brian Rasmussen