Difference between revisions of "ListUnique"

From Second Life Wiki
Jump to navigation Jump to search
(++i is faster than i++. Just a little.)
Line 13: Line 13:
     list lFiltered = llList2List(lAll, 0, 0);
     list lFiltered = llList2List(lAll, 0, 0);
     integer iAll = llGetListLength( lAll );
     integer iAll = llGetListLength( lAll );
     for (i = 1; i < iAll; i++) {
     for (i = 1; i < iAll; ++i) {
         if ( llListFindList(lFiltered, llList2List(lAll, i, i) ) == -1 ) {
         if ( llListFindList(lFiltered, llList2List(lAll, i, i) ) == -1 ) {
             lFiltered += llList2List(lAll, i, i);
             lFiltered += llList2List(lAll, i, i);

Revision as of 17:29, 14 August 2008

Function: list ListUnique(list lAll);

Given a list of elements, strips out any duplicates in that list, and returns the de-duped list.

See also: Lists


<lsl> list ListUnique( 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;

}

</lsl>


Example:

list mylist = ListUnique(["A", "A", "B", "C", "C", "B"])

would return the list:

["A", "B", "C"]


Originated in a November 2004 thread led by Chromal Brodsky in the SL Scripting Forum. http://forums.secondlife.com/showthread.php?t=28137