Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automatic generation of usernames using first initial last name

Status
Not open for further replies.

xsnByron

Programmer
May 23, 2003
3
0
0
US
Hey,

I'm trying to figure out an easy way to modify a user account creation script I've put together to where the user's first and last name are collected and used to generate a username for them.. (instead of allowing them to supply their own..)

e.g. John Smith would be created as jsmith.
John Smith #2 would then be created as jsmith2.

Any simple ideas on how I can strip the form data down to the first letter? And how I should append numbers to create unique names when necessary?
 
[tt]sub realname_to_logname {
# given a realname, return an available logname
my $name = lc($_[0]);
my @names = split( /\s+/, $name);# into words
my $logname = substr( $names[0], 0, 1) . $names[$#names];
my $try = getpwnam( $logname ); # non-zero if $logname exists
my $i=2; # avoid 1 - it looks like letter el

while ( $try ) {
$logname =~ s/(\d*)$/sprintf("%d",$i++)/e;
$try = getpwnam( $logname );
}
return $logname;
}

while(<>){
print realname_to_logname($_), &quot;\n&quot;;
}[/tt]


Have fun,


fish
 
One thing I forgot to mention, as I thought it wouldn't matter.. is that I'm trying to perform this task on a Win32 machine (Windows 2000 specifically) using Perl, and not on a Unix-based OS...

I noticed that getpwnam is an unimplemented function in my version of ActivePerl, and further research has led me to believe that I won't be able to use the function on a windows machine.

Any suggestions/alternatives for this problem that may allow me to make good use of the snippet I've see above?
 
I have researched the topic on CPAN a little, but I haven't had much luck with finding code that can manipulate a supplied text string and strip off the first character, while appending an entire second string into one final string.... It's basically sounding like a (chop) command that will actually be able to chop any amount of the name ..(regardless of letters or character length...)

Any suggestions on how to do this? Also, I want to restate that I'm trying this on a Windows 2000 platform, and I'm not trying to create Windows usernames, but rather simple names and passwords in a textfile...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top