Library: Function: list uniq( list );
|
Chromal Brodsky
ExperimentalMetaphysicist
Join date: 24 Feb 2004
Posts: 243
|
11-23-2004 10:06
Given a list of elements, returns a list of only the unique individual elements. e.g.: uniq(["A", "A", "B", "C", "C", "B"]) would return the list: ["A", "B", "C"] // Given a list, returns only a list of unique elements in source list. list uniq( list lAll ) { integer i; list lFiltered = llList2List(lAll, 0, 0); integer iAll = llGetListLength( lAll );
for (i = 1; i < iAll; i++) { if ( llListFindList(lFiltered, llList2List(lAll, i, i) ) == -1 ) { lFiltered += llList2List(lAll, i, i); } } return lFiltered; }
Any thoughts on llList2List[llList, n, n] vs [ llList2String(lList, n) ] ?
|
Christopher Omega
Oxymoron
Join date: 28 Mar 2003
Posts: 1,828
|
11-25-2004 10:27
From: Chromal Brodsky Any thoughts on llList2List[llList, n, n] vs [ llList2String(lList, n) ] ? The former preserves the type information of the element, whereas the latter does not. As LSL's llList2* functions preform different things depending on the type of the element, its best to preserve this type information.
|
Tread Whiplash
Crazy Crafter
Join date: 25 Dec 2004
Posts: 291
|
Another method...
02-10-2005 12:12
If you only need to loop through a list of homogenous data and get its unique values, this may be a tad faster (though I haven't benchmarked it): (Note this is currently set up to compare integers; but you could use the same layout for strings or floats) //Sort list, for finding unique values... myList = llListSort(myList, 0, 1);
//Set variables important for this process... integer listLen = llGetListLength(myList); integer lastValue; integer uniqueValues; integer i = 0;
while(i < listLen) { integer currValue = llList2Integer(myList, i);
if(currValue != lastValue || i == 0) { //DO SOMETHING IN HERE
uniqueValues++; lastValue = currValue; }
i++; }
llWhisper(0, "I counted "+ (string)uniqueValues +" unique items in that list.");
Enjoy! Take care, --Noel "HB" Wade (Tread Whiplash)
|
SuezanneC Baskerville
Forums Rock!
Join date: 22 Dec 2003
Posts: 14,229
|
a silly comment
02-10-2005 16:55
Given the features avatar bodies are equipped with, shouldn't a uniq list return the names or keys of all male avatars?
_____________________
-
So long to these forums, the vBulletin forums that used to be at forums.secondlife.com. I will miss them.
I can be found on the web by searching for "SuezanneC Baskerville", or go to
http://www.google.com/profiles/suezanne
-
http://lindenlab.tribe.net/ created on 11/19/03.
Members: Ben, Catherine, Colin, Cory, Dan, Doug, Jim, Philip, Phoenix, Richard, Robin, and Ryan
-
|
Chaz Longstaff
Registered User
Join date: 11 Oct 2006
Posts: 685
|
08-01-2007 20:36
hot! thanks for this! I was hoping someone would have covered this and wham found it in 5 seconds! amazing.
|
Very Keynes
LSL is a Virus
Join date: 6 May 2006
Posts: 484
|
01-14-2008 05:33
You may find this useful too:  It should work with all data types, though I use it on keys mostly. Sample output is: new: Elements in x = a, b, c, d new: Elements in y = a, 1, b, 2, c, 3 new: Elements in x or y = a, b, c, d, 1, 2, 3 new: Elements in both x and y = a, b, c new: Elements that are in x but not in y = d new: Elements that are in y but not in x = 1, 2, 3 new: Elements that are in x or y but not both = d, 1, 2, 3 new: list x == y = 0 new: list x == x = 1 new: list y != x = 1 new: list y != y = 0
|