Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Discussion: nnKey2FirstName - Turns a key into a first name

Nepenthes Ixchel
Broadly Offended.
Join date: 6 Dec 2005
Posts: 696
03-26-2006 00:03
Sometimes it's nice to use an avatar's first name only, instead of their entire name. This simple function will take a key and return an avatars first name.

CODE


//////////////////////////////////////////////////////////
//
// nnKey2FirstName
// By Nepenthes Ixchel
//
// arguments:
// (key) id: the id of the aganet to return the firstname of.
//
// Return value:
// (string) The agents first name (all text before the first space, or entire name if there is no space.)
//
// License:
// This code is free to use for any purpose provided a comment of some sort is included in
// the code attributing it to Nepenthes Ixchel. (even if the script is distributed no-mod)

string nnKey2FirstName(key id)
{
return (llGetSubString(llKey2Name(id),0,-1+llSubStringIndex(llKey2Name(id)," ")));
}



////////////////////////////
//
// simple testbed code

default
{

touch_start(integer total_number)
{
llSay(0, "Hello "+nnKey2FirstName(llDetectedKey(0)));
}
}

Nada Epoch
The Librarian
Join date: 4 Nov 2002
Posts: 1,423
Original Thread
03-27-2006 07:03
/15/75/96085/1.html
_____________________
i've got nothing. ;)
Rodrick Harrington
Registered User
Join date: 9 Jul 2005
Posts: 150
03-27-2006 11:01
Note: the same restrictions as llKey2Name apply, ie. in sim only.
Adman Drake
Registered User
Join date: 9 Feb 2006
Posts: 96
03-27-2006 12:31
Don't get me wrong, it's great to share your scripting knowledge with everyone. I'm all for teaching fellow scripters (especially newbies) some tricks and shortcuts.

BUT...

With regard to your "license", do we really have to credit you with your amazing discovery? What if we don't?

I, being a super code, discovered that instead of doing this:

key owner = llGetOwner();
llGiveMoney(owner, amount);

... I can just do this instead:

llGiveMoney(llGetOwner(), amount);

Can I claim that line of code as my own, and "license" it out?

Don't take this criticism too harshly, but seriously, where is the line drawn?

Adman
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
03-27-2006 14:14
From: Adman Drake
With regard to your "license", do we really have to credit you with your amazing discovery? What if we don't?

The following code is released to the public domain:
CODE

string asKeyToFirstName(key id)
{
return llList2String(llParseList2String(llKey2Name(id),[" "],[]),0);
}
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
03-27-2006 15:39
CODE

string sKey2FirstName(string id)
{
return llDeleteSubString(id = llKey2Name(id), llSubStringIndex(id + " ", " "), 0x4000);
}
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Nepenthes Ixchel
Broadly Offended.
Join date: 6 Dec 2005
Posts: 696
03-27-2006 23:34
From: Adman Drake
Don't get me wrong, it's great to share your scripting knowledge with everyone. I'm all for teaching fellow scripters (especially newbies) some tricks and shortcuts.


Yes, because that's the header I grabbed to cut & paste when writing the code. Oops. Let me fix that and release into the public domain:
CODE

//////////////////////////////////////////////////////////
//
// nnKey2FirstName
// By Nepenthes Ixchel
//
// arguments:
// (key) id: the id of the aganet to return the firstname of.
//
// Return value:
// (string) The agents first name (all text before the first space, or entire name if there is no space.)
//
// License:
// released into public domain.

string nnKey2FirstName(key id)
{
return (llGetSubString(llKey2Name(id),0,-1+llSubStringIndex(llKey2Name(id)," ")));
}



////////////////////////////
//
// simple testbed code

default
{

touch_start(integer total_number)
{
llSay(0, "Hello "+nnKey2FirstName(llDetectedKey(0)));
}
}


If someone could update the Library entry as well that would be nice.
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
03-28-2006 11:09
Nitpicking, yes, but also curious...
From: Strife Onizuka
CODE

string sKey2FirstName(string id)
{
return llDeleteSubString(id = llKey2Name(id), llSubStringIndex(id + " ", " "), 0x4000);
}
You're going to make Niklaus Wirth cry like a baby. That should work, but it's pretty evil... string id and assigning to id and depending on execution order?

Plus, according to the Wiki "This function [llSubStringIndex] is relatively slow." Are you sure llParseString2List() isn't faster?

Also, I assume you're trying to optimise by using 0x4000 instead of -1, but you're losing that on adding " " to the substring... the only time llKey2Name won't return a string containing a space is when it fails. Is llDeleteSubString("",-1,-1) a problem?