Difference between revisions of "ListUnique"
Jump to navigation
Jump to search
(New page: {{LSL Header}} __NOTOC__ <div id="box"> == Function: list ListUniq(list {{LSL Param|lAll }}; == <div style="padding: 0.5em;"> Given a list of elements, strips out duplicates in tha...) |
m (<lsl> tag to <source>) |
||
(10 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
{{LSL Header}} __NOTOC__ | {{LSL Header}} __NOTOC__ | ||
<div id="box"> | <div id="box"> | ||
== Function: [[list]] | == Function: [[list]] ListUnique([[list]] {{LSL Param|lAll) }}; == | ||
<div style="padding: 0.5em;"> | <div style="padding: 0.5em;"> | ||
Given a list of elements, strips out duplicates in that list. | Given a list of elements, strips out any duplicates in that list, and returns the de-duped list. | ||
See also: [[List|Lists]] | |||
< | <source lang="lsl2"> | ||
list | list ListUnique( list lAll ) { | ||
integer i; | integer i; | ||
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; | 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); | ||
Line 18: | Line 22: | ||
} | } | ||
</ | </source> | ||
Example:<br /> | Example:<br /> | ||
list mylist = | list mylist = ListUnique(["A", "A", "B", "C", "C", "B"]) | ||
would return the list: | would return the list: | ||
Line 32: | Line 36: | ||
</div> | </div> | ||
Originated in a November 2004 thread led by Chromal Brodsky in the SL Scripting Forum. http://forums-archive.secondlife.com/54/30/28137/1.html | |||
<div> | |||
A *possibly* faster replacement (at least in my experiments in removing duplicate Agent UUIDs from multiple scanners on a sim): | |||
{{LSLC|Examples| | <source lang="lsl2"> | ||
list nvStripDupes(list DupedList) | |||
{ | |||
integer pos; | |||
list Element; | |||
while (llGetListLength(DupedList) != 0) | |||
{ | |||
Element = llList2List(DupedList, 0, 0); | |||
NoDupes += Element; | |||
pos = llListFindList(DupedList, Element); | |||
while ( pos != -1) | |||
{ | |||
DupedList = llDeleteSubList(DupedList, pos, pos); | |||
pos = llListFindList(DupedList, Element); | |||
} | |||
} | |||
return NoDupes; | |||
} | |||
</source> | |||
{{LSLC|Examples|ListUnique}} | |||
[[Category:LSL_User-Defined Functions]] |
Latest revision as of 15:23, 22 January 2015
LSL Portal | Functions | Events | Types | Operators | Constants | Flow Control | Script Library | Categorized Library | Tutorials |
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
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;
}
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-archive.secondlife.com/54/30/28137/1.html
A *possibly* faster replacement (at least in my experiments in removing duplicate Agent UUIDs from multiple scanners on a sim):
list nvStripDupes(list DupedList)
{
integer pos;
list Element;
while (llGetListLength(DupedList) != 0)
{
Element = llList2List(DupedList, 0, 0);
NoDupes += Element;
pos = llListFindList(DupedList, Element);
while ( pos != -1)
{
DupedList = llDeleteSubList(DupedList, pos, pos);
pos = llListFindList(DupedList, Element);
}
}
return NoDupes;
}