Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Programmatically sorting vectors in a list

Debbie Trilling
Our Lady of Peenemünde
Join date: 17 Oct 2006
Posts: 434
09-16-2007 15:06
Hi

Am attempting to sort a randomly-organised list of colour vectors into broad R, G and B groupings, That is, I'm looking for the sort to first consider the x element of the vectors, then the y and then the z.

However, what appears to be happening is that the sort is considering each vector in the list as a whole, possibly as a text-string, rather than looking at each individual of the listed vector.

Now, this is not entirely a surprise or unexpected! I've therefore started a seperate exercise of sorting the vectors using Excel for pasting back into the script. But I was interested if anyone knew of a programmatic solution, for a learning exercise as much as anything else as I could not see another post on this subject, nor a wiki entry.

From: someone

list ColourVector = [<1.000,1.000,1.000>,<0.000,0.000,0.000>,<1.000,0.000,0.000>,<0.000,1.000,0.000>,<0.000,0.000,1.000>,<0.800, 0.000,0.000>,<0.000,0.800,0.000>,<0.000,0.000,0.800>];

default
{

state_entry()
{
ColourVector = llListSort(ColourVector,1,FALSE);
}

}


Above llListSort() orders list ColourVector as:

1.000, 1.000, 1.000 White
0.000, 0.000, 1.000 Blue
0.000, 1.000, 0.000 Green
1.000, 0.000, 0.000 Red
0.000, 0.000, 0.800 Blue-tone
0.000, 0.800, 0.000 Green-tone
0.800, 0.000, 0.000 Red-tone
0.000, 0.000, 0.000 Black

whereas what I'm looking for a sort order of:

1.000, 1.000, 1.000 White
1.000, 0.000, 0.000 Red
0.800, 0.000, 0.000 Red-tone
0.000, 1.000, 0.000 Green
0.000, 0.800, 0.000 Green-tone
0.000, 0.000, 1.000 Blue
0.000, 0.000, 0.800 Blue-tone
0.000, 0.000, 0.000 Black


I suspect that there may be a solution in declaring the list as

From: someone

list ColourVector = ["1.000","1.000","1.000","0.000","0.000","0.000",........];

and then using a stride of 3, but haven't yet tried this out. And even if this did correctly do the sort, further complications would then arise in attempting to re-build/concatenate the three seperate elements of each vector for use in functions such as llSetText() & llSetColor().

OK, tea-break over..back to scripting :)
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
09-16-2007 16:27
OK I am missing something. The script you posted sorts and gives the result you say you wanted, not the result you said it gives.

CODE

list ColourVector = [<1.000,1.000,1.000>,<0.000,0.000,0.000>,<1.000,0.000,0.000>,<0.000,1.000,0.000>,<0.000,0.000,1.000>,<0.800, 0.000,0.000>,<0.000,0.800,0.000>,<0.000,0.000,0.800>];

default
{

touch_start(integer total_number)
{
ColourVector = llListSort(ColourVector,1,FALSE);
llOwnerSay((string)ColourVector);
}

}


returns: <1.00000, 1.00000, 1.00000><1.00000, 0.00000, 0.00000><0.80000, 0.00000, 0.00000><0.00000, 1.00000, 0.00000><0.00000, 0.80000, 0.00000><0.00000, 0.00000, 1.00000><0.00000, 0.00000, 0.80000><0.00000, 0.00000, 0.00000>
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime.
From: someone
I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
Debbie Trilling
Our Lady of Peenemünde
Join date: 17 Oct 2006
Posts: 434
09-16-2007 18:08
Thanks for having a look at this Jesse

I took your code & pasted it into a 'New Script' of a newly created object. The results I obtained were the same as those I orginally posted, and different to yours. How strange!

[17:59] Object: Hello, Avatar!
[18:00] Object: <1.000000, 1.000000, 1.000000><0.000000, 0.000000, 1.000000><0.000000, 1.000000, 0.000000><1.000000, 0.000000, 0.000000><0.000000, 0.000000, 0.800000><0.000000, 0.800000, 0.000000><0.800000, 0.000000, 0.000000><0.000000, 0.000000, 0.000000>

Don't know why this is, but have duplicated these results twice now using your code, and numerous times this evening using my own (and before I decided to post a thread on the forum). Wish I was getting your results! Would save me hours in Excel :)
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
09-16-2007 18:14
Crop!!!! Sorry Debbie, although I use Scite, I have taken to playing with LSLEditor lately. I ran it through there and it gave the correct results. Just tried it in world and got your results. You might want to file a JIRA on this one. Looks like they need to straighten out some code.
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime.
From: someone
I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
Debbie Trilling
Our Lady of Peenemünde
Join date: 17 Oct 2006
Posts: 434
09-16-2007 18:24
Thanks Jesse! Will do! :)
Qie Niangao
Coin-operated
Join date: 24 May 2006
Posts: 7,138
09-16-2007 19:03
In the meantime, sorting a string typecast version of the list seems to give the desired order, as with:

CODE

list ColourVector = [<1.000,1.000,1.000>,<0.000,0.000,0.000>,<1.000,0.000,0.000>,<0.000,1.000,0.000>,<0.000,0.000,1.000>,<0.800, 0.000,0.000>,<0.000,0.800,0.000>,<0.000,0.000,0.800>];

list stringList;

default
{

state_entry()
{
integer listLen = llGetListLength(ColourVector);
integer listIdx;
for (listIdx = 0; listIdx < listLen; listIdx +=1)
stringList += (string)llList2Vector(ColourVector, listIdx);
stringList = llListSort(stringList, 1, FALSE);
ColourVector = [];
for (listIdx = 0; listIdx < listLen; listIdx +=1)
ColourVector += (vector)llList2String(stringList,listIdx);
//ColourVector = llListSort(ColourVector,1,FALSE);
llOwnerSay(llDumpList2String(ColourVector, ", "));
}
}


There's likely some less hideous way to do it, but the results seem as desired.
Debbie Trilling
Our Lady of Peenemünde
Join date: 17 Oct 2006
Posts: 434
Jira #misc-696
09-17-2007 12:17
Thx Qie, will slip this into a script later tonight & get back to you :)

JIRA raised for this issue: https://jira.secondlife.com/browse/MISC-696
Debbie Trilling
Our Lady of Peenemünde
Join date: 17 Oct 2006
Posts: 434
09-17-2007 13:37
Hi Qie and thanks for your proposed solution.

The solution does indeed correctly 'programmatically sort vectors in a list' and so the challenge has been met! Well done :)

I am working on a little 'SL Artist's Colour Wheel' utilty ~ cycling thro' colour vectors & displaying basic information. The difficulty with the proposed solution, I think, is that it is quite memory-greedy. In order to avoid a 'Stack-Heap Colliion' immediately after compiling, I needed to cull the number of vectors contained within the concatenated lists from 200-odd down to 70-odd (ie: I was left with a single list of vectors, as opposed to just under 3 lists worth).

So back to my Excel spreadsheets, but thanks so much for your efforts!