Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Finding "not a" in a list

Rolig Loon
Not as dumb as I look
Join date: 22 Mar 2007
Posts: 2,482
08-03-2009 12:46
It's easy to find "b" in a list that looks like ["a","a","a","b","a","a","a"] using llListFindList. What if I don't care what "b" is, but I want to find the index of the element that is not "a"? Is there a more elegant way to do it than by looping through the list like this? ........

CODE

default
{
touch_start(integer total_number)
{
list items = ["a","a","a","b","a","a","a"];
integer len = llGetListLength(items)-1;
llOwnerSay((string)llListFindList(items,["b"])); //This finds "b" directly

string match = llList2String(items,len);
//In this small test, I know that the last element in the list is "a", so let's find "not a"
while (len--)
{
llOwnerSay("Position = "+(string)len+" Value = "+llList2String(items, len));
if(llList2String(items,len) != match)
{
integer pos = len;
llOwnerSay("Mismatch found at postion "+(string)pos); // Here it is!
}
}
}
}
_____________________
It's hard to tell gender from names around here but if you care, Rolig = she. And I exist only in SL, so don't ask.... ;)

Look for my work in XStreetSL at
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
08-03-2009 12:47
Don't think so. Just use a loop.
Rolig Loon
Not as dumb as I look
Join date: 22 Mar 2007
Posts: 2,482
08-03-2009 13:12
Thanks. That's what I figured. I was just hoping there was some nifty shortcut like llListFindList(items,llNotInList(["a"]) ) or llFindNotInList(items, ["a"]). :p
_____________________
It's hard to tell gender from names around here but if you care, Rolig = she. And I exist only in SL, so don't ask.... ;)

Look for my work in XStreetSL at
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
08-03-2009 20:06
CODE

integer uLstNotBeg( list vLstSrc, list vLstNot ){
return
llListFindList( vLstSrc,
(list)llListt2String(
llParseString2List(
llDumpList2String( vLstSrc, ";" ),
vLstNot + (list)";", [] ),
0 ) );
}


no testing, but I think that's the right logic for not...

dumps the list, rebuilds it w/o your Not elements, then grabs the first element of that new list and looks for it in the original to get the index.

only works for strings, Not can only be up to 7 elements, which will exclude each element separately. suggest replacing ";" with the alt+7 character. use -1 instead of 0 if you want the last Not.

ETA:
note it's quicker to actually get the value if you don't actually need the index, simply rebuild the list without your excluded element, which if looped by replacing the not with the first element can get you all the unique elements... and only use the above for potentially large lists
_____________________
|
| . "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...
| -
Briana Dawson
Attach to Mouth
Join date: 23 Sep 2003
Posts: 5,855
08-03-2009 20:08
*watches the wizards at work*
_____________________
WooT
------------------------------

http://www.secondcitizen.net/Forum/
Rolig Loon
Not as dumb as I look
Join date: 22 Mar 2007
Posts: 2,482
08-03-2009 20:59
From: Void Singer
CODE

integer uLstNotBeg( list vLstSrc, list vLstNot ){
return
llListFindList( vLstSrc,
(list)llListt2String(
llParseString2List(
llDumpList2String( vLstSrc, ";" ),
vLstNot + (list)";", [] ),
0 ) );
}


no testing, but I think that's the right logic for not...

dumps the list, rebuilds it w/o your Not elements, then grabs the first element of that new list and looks for it in the original to get the index.

only works for strings, Not can only be up to 7 elements, which will exclude each element separately. suggest replacing ";" with the alt+7 character. use -1 instead of 0 if you want the last Not.

ETA:
note it's quicker to actually get the value if you don't actually need the index, simply rebuild the list without your excluded element, which if looped by replacing the not with the first element can get you all the unique elements... and only use the above for potentially large lists

Ah, now THAT's the sort of magic I was hoping for. :) Thanks, Void!
_____________________
It's hard to tell gender from names around here but if you care, Rolig = she. And I exist only in SL, so don't ask.... ;)

Look for my work in XStreetSL at
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
08-03-2009 21:45
just realized that -1 will probably won't work for the last Not, but applying the find reverse I have posted to the forum and my wiki page, will (it mightn't work because there's no guarantee that the last Not element is unique)
_____________________
|
| . "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...
| -
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
08-03-2009 23:06
Err...okay. That's better than a simple loop? :p
Dora Gustafson
Registered User
Join date: 13 Mar 2007
Posts: 779
08-04-2009 03:16
From: Hewee Zetkin
Err...okay. That's better than a simple loop? :p
Let them play, don't spoil the fun:)
_____________________
From Studio Dora
Rolig Loon
Not as dumb as I look
Join date: 22 Mar 2007
Posts: 2,482
08-04-2009 05:59
A simple loop works just fine, but gee ...... anybody can do a simple loop. Where's the fun in that? :D
_____________________
It's hard to tell gender from names around here but if you care, Rolig = she. And I exist only in SL, so don't ask.... ;)

Look for my work in XStreetSL at
Briana Dawson
Attach to Mouth
Join date: 23 Sep 2003
Posts: 5,855
08-04-2009 06:01
From: Rolig Loon
A simple loop works just fine, but gee ...... anybody can do a simple loop. Where's the fun in that? :D


I CAN'T!
_____________________
WooT
------------------------------

http://www.secondcitizen.net/Forum/
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
08-04-2009 12:48
From: Hewee Zetkin
Err...okay. That's better than a simple loop? :p

depends on how big the list is =P

for a large list it's potentially faster... not that I did any averaging tests or anything, I wrote it in the reply. if there are a lot of "not" values (or the ones fed in cover most of the entries) it should be more effecient processing wise. dunno how it compares bytecode-wise.
_____________________
|
| . "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...
| -