User:Dora Gustafson/occurences

From Second Life Wiki
< User:Dora Gustafson
Revision as of 08:30, 31 March 2014 by Dora Gustafson (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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>