Is there a way to do like
A List of 20 items, then ask LSL if one of the 20 items is called say "apple" and if so return it's index?
I know how to it with a loop but is there an easyer way?
These forums are CLOSED. Please visit the new forums HERE
Searching a List |
|
Nyx Alsop
Registered User
Join date: 14 Dec 2008
Posts: 252
|
12-14-2009 03:48
Is there a way to do like
A List of 20 items, then ask LSL if one of the 20 items is called say "apple" and if so return it's index? I know how to it with a loop but is there an easyer way? |
Pete Olihenge
Registered User
![]() Join date: 9 Nov 2009
Posts: 315
|
12-14-2009 04:04
llListFindList at http://wiki.secondlife.com/wiki/LlListFindList should do it.
|
Haravikk Mistral
Registered User
Join date: 8 Oct 2005
Posts: 2,482
|
12-14-2009 10:10
As Pete points out; llListFindList() is the way. If you ever find that you can't use llListFindList() for some reason, then you're pretty much always better off finding a way, either by creating a strided list or (usually better) a second "index" list.
Searching with a loop, though fine in most normal languages, is really slow in LSL compared to using llListFindList(). Even methods like binary search for sorted data is still quite a bit slower; they would in theory be faster with really huge data-sets, but these would be bigger than even Mono scripts can handle so that point is moot. The only faster method I've found is the use of bitfields to index a list of boolean data, but that restricts a list to 32 entries maximum, but can be handy depending on your use-case. Sorry for the aside; it's probably way more information than you're looking for, but it's useful to know that at this time llListFindList is the fastest method for pretty much every case where you want to find something in a list. _____________________
Computer (Mac Pro):
2 x Quad Core 3.2ghz Xeon 10gb DDR2 800mhz FB-DIMMS 4 x 750gb, 32mb cache hard-drives (RAID-0/striped) NVidia GeForce 8800GT (512mb) |
Void Singer
Int vSelf = Sing(void);
![]() Join date: 24 Sep 2005
Posts: 6,973
|
12-14-2009 12:46
list gLstSource = ["oranges", "apples", "pears"];
x = llListFindList( gLstSource, (list)"apples" ); //-- returns 1, the index of the first found matching item if (~llListFindList( gLstSource, (list)"apples" ) //-- evaluates to true spelling counts when searching "apples" != "Apples" there's also some functions linked to on the referenced page that allow you to search for the last index, or search for any of multiple items at once, written by yours truly. _____________________
|
| . "Cat-Like Typing Detected" | . This post may contain errors in logic, spelling, and | . grammar known to the SL populace to cause confusion | | - Please Use PHP tags when posting scripts/code, Thanks. | - Can't See PHP or URL Tags Correctly? Check Out This Link... | - |
Nyx Alsop
Registered User
Join date: 14 Dec 2008
Posts: 252
|
12-14-2009 16:17
As Pete points out; llListFindList() is the way. If you ever find that you can't use llListFindList() for some reason, then you're pretty much always better off finding a way, either by creating a strided list or (usually better) a second "index" list. Searching with a loop, though fine in most normal languages, is really slow in LSL compared to using llListFindList(). Even methods like binary search for sorted data is still quite a bit slower; they would in theory be faster with really huge data-sets, but these would be bigger than even Mono scripts can handle so that point is moot. The only faster method I've found is the use of bitfields to index a list of boolean data, but that restricts a list to 32 entries maximum, but can be handy depending on your use-case. Sorry for the aside; it's probably way more information than you're looking for, but it's useful to know that at this time llListFindList is the fastest method for pretty much every case where you want to find something in a list. It was very useful, thank you. |