Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Discussion: Useful (I hope) List Logic Functions

Very Keynes
LSL is a Virus
Join date: 6 May 2006
Posts: 484
06-17-2007 18:08
Whilst develeping a reletivly complex script I descoverd that, although extensive, the list manipulaton functions in LSL were lacking logical functions, especialy those that can be used in Set Theory, so I developed the following functions to solve my problem and hope they are of use to others.
CODE

//list logic
//By Very Keynes - Please use or modify freely
//
list l1 = ["a","b","c","d"]; //test lists
list l2 = ["a","1","b","2","c","3"];

list ListXorY(list lx, list ly) // add the lists eleminating duplicates
{
list lz = []; integer x;
for (x = 0; x < llGetListLength(ly); ++x)
{
if (~llListFindList(lx,llList2List(ly,x,x))){;}
else {lz = lz + llList2List(ly,x,x);}
}
return lx + lz;
}

list ListXandY(list lx, list ly) // return a list of elements common to both lists
{
list lz = []; integer x;
for (x = 0; x < llGetListLength(ly); x++)
{
if (~llListFindList(lx,llList2List(ly,x,x))){lz = lz + llList2List(ly,x,x);}
else {;}
}
return lz;
}

list ListXnotY(list lx, list ly) // return elements in X that are not in Y
{
list lz = []; integer x;
for (x = 0; x < llGetListLength(lx); x++)
{
if (~llListFindList(ly,llList2List(lx,x,x))){;}
else {lz = lz + llList2List(lx,x,x);}
}
return lz;
}

list ListXxorY(list lx, list ly) // reteurn elements that are in X or Y but not both
{
return ListXnotY( lx, ly) + ListXnotY( ly, lx);
}

integer ListXequY(list lx, list ly)
{
if(llList2CSV(ListXnotY(lx,ly))=="")return TRUE;
else return FALSE;
}

integer ListXneqY(list lx, list ly)
{
if(llList2CSV(ListXnotY(lx,ly))=="")return FALSE;
else return TRUE;
}

// Code used to test the above

default
{
state_entry()
{
llOwnerSay("testing functions");
llOwnerSay("Elements in x = " + llList2CSV(l1));
llOwnerSay("Elements in y = " + llList2CSV(l2));
llOwnerSay("Elements in x or y = " + llList2CSV(ListXorY(l1,l2)));
llOwnerSay("Elements in both x and y = " + llList2CSV(ListXandY(l1,l2)));
llOwnerSay("Elements that are in x but not in y = " + llList2CSV(ListXnotY(l1,l2)));
llOwnerSay("Elements that are in y but not in x = " + llList2CSV(ListXnotY(l2,l1)));
llOwnerSay("Elements that are in x or y but not both = " + llList2CSV(ListXxorY(l1,l2)));
llOwnerSay("list x == y = " + (string)ListXequY(l1,l2));
llOwnerSay("list x == x = " + (string)ListXequY(l1,l1));
llOwnerSay("list y != x = " + (string)ListXneqY(l2,l1));
llOwnerSay("list y != y = " + (string)ListXneqY(l2,l2));

}
}
Nada Epoch
The Librarian
Join date: 4 Nov 2002
Posts: 1,423
Original Thread
06-29-2007 00:18
/15/ac/191527/1.html
_____________________
i've got nothing. ;)
Senuka Harbinger
A-Life, one bit at a time
Join date: 24 Oct 2005
Posts: 491
06-29-2007 07:04
Wow. Incredibly useful learning tool for me, as data manipulation with LSL has always been my weakest spot.
_____________________
My SLExchange shop

Typos are forgiven; desecrating the english language with reckless abandon and necrophilic acts is not.


The function is working perfectly fine. It's just not working the way you wanted it to work.
Kidd Krasner
Registered User
Join date: 1 Jan 2007
Posts: 1,938
06-29-2007 19:07
I suggest not confusing boolean and set operators. It's union, not or, intersection, not and.
Espresso Saarinen
old geek
Join date: 22 Jan 2006
Posts: 93
07-03-2007 12:17
uh, this stuff does not compile for me

if ((integer)~llListFindList(lx,llList2List(ly,x,x)))

barfs at the ~
Very Keynes
LSL is a Virus
Join date: 6 May 2006
Posts: 484
07-12-2008 12:25
From: Espresso Saarinen
uh, this stuff does not compile for me

if ((integer)~llListFindList(lx,llList2List(ly,x,x)))

barfs at the ~

the type casting was for compatability with an earlyer version of LSLEditor, I have edited the code to remove them and it should compile and run fine
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
07-12-2008 12:32
I believe the (in)equality functions are incorrect. You test whether ListXnotY() is (non)empty, but if there were elements in Y that are not in X, it would consider the lists equal. You might want to use your ListXxorY() there instead.