Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

llStringCount

Oz Spade
ReadsNoPostLongerThanHand
Join date: 23 Sep 2003
Posts: 2,708
02-16-2005 03:47
May have been suggested once before, but I couldn't find anything on it, basicly the following:

From: someone

integer llStringCount(string source, string test)

Counts how many times test occures in source and returns total.


Alternatively it could be expanded for integers or lists... Integers I'm not sure how usefull that'd actualy be, but I can see a list count being usefull as well. i.e.:

From: someone

integer llListCount(list source, list/or/string test)

Counts how many times test occures in source and returns total.


Edit to add: To make a more "Global" function, you could do:

From: someone

integer llCount(integer type, list/string/integer source, list/string/integer test)

Type determines if the target variable is list/string/integer, then counts how many times test occures in source and returns total.


But... not sure how possible a multi-accepting variable type function would be.
_____________________
"Don't anticipate outcome," the man said. "Await the unfolding of events. Remain in the moment." - Konrad
Jeffrey Gomez
Cubed™
Join date: 11 Jun 2004
Posts: 3,522
02-16-2005 07:43
Considering the existing code could look something like this:

CODE
integer StringCount(string src, string test)
{
integer count = 0;
while(llSubStringIndex(src, test) != -1)
{
count += 1;
src = llDeleteSubString(src,0,llSubStringIndex(str,test));
// Note: Code may need to account for the length of string test in llDeleteSubString
// I've not had a chance to test multi-character returns with the command
// Either way, llStringLength(test) would solve that.
// src = llDeleteSubString(src,0,llSubStringIndex(str,test) + llStringLength(test) - 1);
}
return count;
}


And this:

CODE
integer ListCount(list src, list test)
{
integer count = 0;
while(llListFindList(src, test) != -1)
{
count += 1;
src = llDeleteSubList(src,0,llListFindList(str,test));
// Note: Likewise here. May need to account for length of "test"
// llGetListLength would solve that problem
// src = llDeleteSubList(src,0,llListFindList(str,test) + llGetListLength(test) - 1);
}
return count;
}


... I see no problem.

Your llCount is slightly obsolete, since list comparisons kinda do that now.

</$0.02>
_____________________
---
Lex Neva
wears dorky glasses
Join date: 27 Nov 2004
Posts: 1,361
02-16-2005 09:07
This could be extended to simply have functions that return a list of all positions that some test string is found in a string, and likewise for a list.

ie:

CODE

llSubStringIndices("1234123412341234","12");
// returns [0,4,8,12]


similar for llListFindListIndices()
Jeffrey Gomez
Cubed™
Join date: 11 Jun 2004
Posts: 3,522
02-16-2005 10:26
Well...

CODE
list StringReturns(string src, string test)
{
list count = [];
while(llSubStringIndex(src, test) != -1)
{
count += llSubStringIndex(src, test);
src = llDeleteSubString(src,0,llSubStringIndex(str,test));
// Note: Code may need to account for the length of string test in llDeleteSubString
// I've not had a chance to test multi-character returns with the command
// Either way, llStringLength(test) would solve that.
// src = llDeleteSubString(src,0,llSubStringIndex(str,test) + llStringLength(test) - 1);
}
return count;
}


CODE
list ListReturns(list src, list test)
{
list count = [];
while(llListFindList(src, test) != -1)
{
count += llListFindList(src, test);
src = llDeleteSubList(src,0,llListFindList(str,test));
// Note: Likewise here. May need to account for length of "test"
// llGetListLength would solve that problem
// src = llDeleteSubList(src,0,llListFindList(str,test) + llGetListLength(test) - 1);
}
return count;
}


Next? :rolleyes:
_____________________
---
Oz Spade
ReadsNoPostLongerThanHand
Join date: 23 Sep 2003
Posts: 2,708
02-17-2005 01:31
Thanks for the code Jeffrey :)

I do still think more string/etc handling functions are needed though, even for simple tasks as with LSL every bit of space you can conserve in the code counts.

This of course may not be one of those tasks as the code to do them isn't extremely long.
_____________________
"Don't anticipate outcome," the man said. "Await the unfolding of events. Remain in the moment." - Konrad
Lex Neva
wears dorky glasses
Join date: 27 Nov 2004
Posts: 1,361
02-17-2005 08:36
Yes, Jeffrey, I realize it can be done in LSL. The problem is that that's woefully inefficient. Trying to program something big that does list processing is a serious pain in the butt, and is likely to fill your memory either with lists or code or both :P