User:Dora Gustafson/occurences
< User:Dora Gustafson
Jump to navigation
Jump to search
Revision as of 08:30, 31 March 2014 by Dora Gustafson (talk | contribs) (Count occurencces in a list)
Count Occurrences in a list
Look for and count anything, that can be an element, in a list
- This script is an example which include a list: "haystack" and an element: "needle"
- Here "haystack" and "needle" are constants and only meant to make up a working script
<lsl> // count occurrences in list; by Dora Gustafson, Studio Dora 2014 // v1.1 inline code
default {
state_entry() { list haystack = [1,2,1,2,1,2,3,2,4,5,2,6,4,2,3,2,1,8]; list needle = [2]; integer i = 0; integer j = 0; integer k = llListFindList( haystack, needle); integer m = llGetListLength( haystack ); while ( k >= 0 && i < m ) { i += k+1; k = llListFindList( llList2List( haystack, i, -1), needle); ++j; } llOwnerSay((string)needle+" occurs "+(string)j+" times in "+llDumpList2String( haystack, ", ")); }
} </lsl>