Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

counting and ordering lists

Silent Bock
Registered User
Join date: 4 May 2006
Posts: 8
05-19-2006 04:11
I have looked every where and can't find anything useful about this. I have a list of items :

["blue apple", "green apple", "bling bling apple", "rotting zombie", "green apple", "green apple", "rotting zombie"];

What would be the best way of counting (and ordering) the items in the list? I am thinking along the lines of looping through the list and creating a new list every time I find a new item, and then adding a count to that list if it occurs again. This does sound rather resource intensive though. I was just wondering if there are any list ordering facilies built into the langauge, rather than write something that has already been written.

Thanks

Silent

p.s. incase the description seems vague, the output I would like would be something like :

["green apple",3]
["rotting zombie",2]
["blue apple",1]
["bling bling apple",1]

hopefully in such a way I can say "ordered_item[0] is the most popular!"
Nick Shatner
Isn't a game
Join date: 11 Jul 2005
Posts: 39
05-19-2006 05:03
I'd recommend having a separate list, and using something like the following:
CODE

list items;
list sales;
increment_sale(string object)
{
integer index = llListFindList(items, [object]);
if(index != -1)
{
integer currentSales = llList2Integer(sales, index);
sales = llDeleteSubList(sales, index, index);
sales = llListInsertList(sales, [++currentSales], index);
}
}
get_sale(string object)
{
integer index = llListFindList(items, [object]);
if(index != -1)
{
integer currentSales = llList2Integer(sales, index);
llSay(0, object + " has sold: " + (string)currentSales + " copies");
}
}
default
{
state_entry()
{
items = ["blue apple", "green apple", "bling bling apple", "rotting zombie", "green apple", "green apple", "rotting zombie"];
integer i;
for(i = 0; i < llGetListLength(items); i++)
{
sales += [0];
}
}
}


Give me a buzz inworld if you want any help with using it - I could also write you a function to combine then sort the lists to find the most popular item.
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
05-19-2006 05:29
I would do it this way, it's a bit ugly but should work.
You may be wondering "Why not use strings to compair vectors, floats & rotations?" LL's float -> string function (that all 3 would be using in that situation) would loose the lower most number which could return false positives. Another more serrious issue, is that LL's sort function cannot see a difference between positive and negitive zero. The only way to identify the sign of a float who's value is zero is to convert it to a string. Because of this, false boundries could be setup. Since none of the LSL functions have a different behavior for negitive zero vs positive zero, I have no regrets about corrupting the sign.

CODE

list count_types(list input)
{
integer len = llGetListLength(input);
list out;
if(len)
{
integer pos = 0;
integer type = llGetListEntryType(input = llListSort(input, 1, TRUE), 0);
integer count = 1;
do
{
integer ttype = llGetListEntryType(input, ++pos);
if(type == ttype)
{
if(type == TYPE_FLOAT)
{
if(llList2Float(input, pos) == llList2Float(input, pos - 1))
jump next1;
}
else if(type == TYPE_ROTATION)
{
if(llList2Rot(input, pos) == llList2Rot(input, pos - 1))
jump next2;
}
else if(type == TYPE_VECTOR)
{
if(llList2Vector(input, pos) == llList2Vector(input, pos - 1))
jump next3;
}
else if(llList2String(input, pos) == llList2String(input, pos - 1))
jump next4;
}
out += (llList2List(input, pos - 1, pos - 1) + count);
count = 0;
type = ttype;
@next1;@next2;@next3;@next4;
++count;
}while(pos < len);
}
return out;
}
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey