Sorting Lists and making Dialogs
|
|
Winter Ventura
Eclectic Randomness
Join date: 18 Jul 2006
Posts: 2,579
|
03-28-2007 18:39
HELP! Okay I'm having a brain fart big time here. avatarsDetected = llListSort(avatarsDetected, 1, TRUE); avatarsDetected = llListSort(avatarsDetected, 3, FALSE); What's wrong with my thinking here? I need to alphabetize the list.. that's easy enough, then I need to break it into groups of three, and reverse alphabetize the list, so the alphabetization reads correctly on the dialog buttons (01) (02) (03) (04) (05) (06) (07) (0  (09) (10) (11) (12) is really [ 10, 11, 12, 07, 08, 09, 04, 05, 06, 01, 02, 03 ] How do I make this happen??? I'm stuck!
_____________________
 ● Inworld Store: http://slurl.eclectic-randomness.com ● Website: http://www.eclectic-randomness.com ● Twitter: @WinterVentura
|
|
Thraxis Epsilon
Registered User
Join date: 31 Aug 2005
Posts: 211
|
03-28-2007 19:01
avatarsDetected = (avatarsDetected = [] ) + llListSort(avatarsDetected, 1, TRUE); avatarsDetected = (avatarsDetected = [] ) + llList2List(avatarsDetected,9,11) + llList2List(avatarsDetected,6,8)+ llList2List(avatarsDetected,3,5)+ llList2List(avatarsDetected,0,2);
|
|
Winter Ventura
Eclectic Randomness
Join date: 18 Jul 2006
Posts: 2,579
|
03-28-2007 19:05
How well does that handle a 4 person list? (not a perfectl 12 item list)
Edit.. Just checked it out in world.. seems to work perfectly. Thanks!
_____________________
 ● Inworld Store: http://slurl.eclectic-randomness.com ● Website: http://www.eclectic-randomness.com ● Twitter: @WinterVentura
|
|
Deanna Trollop
BZ Enterprises
Join date: 30 Jan 2006
Posts: 671
|
03-28-2007 19:54
With 4 items, really? I'd think you'd end up with a menu that looks like: (03) (04)(01)(02)
I fiddled around with this months ago. The only way to get an arbitrary list of buttons truly sorted correctly is, after sorting the list, pad it with blanks until the list length % 3 == 0 [ "01", "02", "03", "04", " ", " " ] then rearrange it in a manner similar Thraxis method [ "04", " ", " ", "01", "02", "03" ] which results in: (01)(02)(03) (04)( )( )
|
|
Thraxis Epsilon
Registered User
Join date: 31 Aug 2005
Posts: 211
|
03-29-2007 14:45
This function will take care of the issue: list menu_list(list input) { while(((llGetListLength(input) % 3) != 0) && (llGetListLength(input) > 3)) { input = (input = []) + input + [" "]; } input = (input = []) + llList2List(input,9,11) + llList2List(input,6,8) + llList2List(input,3,5) + llList2List(input,0,2); return input; }
Below is a test application to show how it works list test1 = ["1","2","3","4","5","6","7","8","9","10","11","12"]; list test2 = ["1","2","3","4","5","6","7","8","9","10","11"]; list test3 = ["1","2","3","4","5","6","7","8","9","10"]; list test4 = ["1","2","3","4","5","6","7","8","9"]; list test5 = ["1","2","3","4","5","6","7","8"]; list test6 = ["1","2","3","4","5","6","7"]; list test7 = ["1","2","3","4","5","6"]; list test8 = ["1","2","3","4","5"]; list test9 = ["1","2","3","4"]; list test10 = ["1","2","3"]; list test11 = ["1","2"]; list test12 = ["1"]; list menu_list(list input) { while(((llGetListLength(input) % 3) != 0) && (llGetListLength(input) > 3)) { input = (input = []) + input + [" "]; } input = (input = []) + llList2List(input,9,11) + llList2List(input,6,8) + llList2List(input,3,5) + llList2List(input,0,2); return input; }
default { touch_start(integer total_number) { llDialog(llGetOwner(),"1",menu_list(test1),1); llDialog(llGetOwner(),"2",menu_list(test2),1); llDialog(llGetOwner(),"3",menu_list(test3),1); llDialog(llGetOwner(),"4",menu_list(test4),1); llDialog(llGetOwner(),"5",menu_list(test5),1); llDialog(llGetOwner(),"6",menu_list(test6),1); llDialog(llGetOwner(),"7",menu_list(test7),1); llDialog(llGetOwner(),"8",menu_list(test8),1); llDialog(llGetOwner(),"9",menu_list(test9),1); llDialog(llGetOwner(),"10",menu_list(test10),1); llDialog(llGetOwner(),"11",menu_list(test11),1); llDialog(llGetOwner(),"12",menu_list(test12),1); } }
|
|
Learjeff Innis
musician & coder
Join date: 27 Nov 2006
Posts: 817
|
03-29-2007 14:53
Of course, if you want to serve your customers, sort it into columns, not rows. (If your head doesn't hurt enough already, that is!) 
|
|
Thraxis Epsilon
Registered User
Join date: 31 Aug 2005
Posts: 211
|
03-29-2007 15:20
Menu Items into Columns: list menu_list_col(list input) { while((llGetListLength(input) % 12) != 0) { input = (input = []) + input + [" "]; } input = (input = []) + llList2ListStrided(llDeleteSubList(input,0,2),0,-1,4) + llList2ListStrided(llDeleteSubList(input,0,1),0,-1,4) + llList2ListStrided(llDeleteSubList(input,0,0),0,-1,4) + llList2ListStrided(input,0,-1,4); return input; }
Demo Script list test1 = ["1","2","3","4","5","6","7","8","9","10","11","12"]; list test2 = ["1","2","3","4","5","6","7","8","9","10","11"]; list test3 = ["1","2","3","4","5","6","7","8","9","10"]; list test4 = ["1","2","3","4","5","6","7","8","9"]; list test5 = ["1","2","3","4","5","6","7","8"]; list test6 = ["1","2","3","4","5","6","7"]; list test7 = ["1","2","3","4","5","6"]; list test8 = ["1","2","3","4","5"]; list test9 = ["1","2","3","4"]; list test10 = ["1","2","3"]; list test11 = ["1","2"]; list test12 = ["1"]; list menu_list(list input) { while(((llGetListLength(input) % 3) != 0) && (llGetListLength(input) > 3)) { input = (input = []) + input + [" "]; } input = (input = []) + llList2List(input,9,11) + llList2List(input,6,8) + llList2List(input,3,5) + llList2List(input,0,2); return input; } list menu_list_col(list input) { while((llGetListLength(input) % 12) != 0) { input = (input = []) + input + [" "]; } input = (input = []) + llList2ListStrided(llDeleteSubList(input,0,2),0,-1,4) + llList2ListStrided(llDeleteSubList(input,0,1),0,-1,4) + llList2ListStrided(llDeleteSubList(input,0,0),0,-1,4) + llList2ListStrided(input,0,-1,4); return input; } default { touch_start(integer total_number) { llDialog(llGetOwner(),"1",menu_list_col(test1),1); llDialog(llGetOwner(),"2",menu_list_col(test2),1); llDialog(llGetOwner(),"3",menu_list_col(test3),1); llDialog(llGetOwner(),"4",menu_list_col(test4),1); llDialog(llGetOwner(),"5",menu_list_col(test5),1); llDialog(llGetOwner(),"6",menu_list_col(test6),1); llDialog(llGetOwner(),"7",menu_list_col(test7),1); llDialog(llGetOwner(),"8",menu_list_col(test8),1); llDialog(llGetOwner(),"9",menu_list_col(test9),1); llDialog(llGetOwner(),"10",menu_list_col(test10),1); llDialog(llGetOwner(),"11",menu_list_col(test11),1); llDialog(llGetOwner(),"12",menu_list_col(test12),1); } }
|
|
Learjeff Innis
musician & coder
Join date: 27 Nov 2006
Posts: 817
|
03-30-2007 09:17
Please explain this line: input = (input = []) + input + [" "];
I say the proper way to write this is input += [" "];
I suspect that you're doing this to be more efficient; showing that the old copy of input can be tossed before appending the new element. How are we assured that the operands for + are evaluated right to left? This code could fail with an SL update. If the compiler generates better code for the first version than the second version, it's an implementation artifact. The semantics of the second is guaranteed, and should really be the most efficient (if the compiler takes advantage or the information available). Thanks for the code in any event.
|
|
Deanna Trollop
BZ Enterprises
Join date: 30 Jan 2006
Posts: 671
|
03-30-2007 12:11
From: someone How are we assured that the operands for + are evaluated right to left? This code could fail with an SL update. As could just about anything.  From: someone The semantics of the second is guaranteed, and should really be the most efficient (if the compiler takes advantage or the information available). But it doesn't. I've tested this out myself, and stack/heap collisions happen much sooner using the second syntax than using the first. Granted, this is really only an issue if the script is running so low on stack space that there's not enough room for an additional copy of a given list.
|
|
Thraxis Epsilon
Registered User
Join date: 31 Aug 2005
Posts: 211
|
03-30-2007 12:45
You're right... it could fail sometime, possibly, maybe.. with a future update. But right now that is the proper method for saving memory when doing list operations, and it can and will give you needed memory even when you are not running close to a stack-heap collision.
|
|
Learjeff Innis
musician & coder
Join date: 27 Nov 2006
Posts: 817
|
03-30-2007 17:59
hmmm, embedding reliance on SL bugs in your code ...
I recommend against a silly construct like this unless you really need it. Which isn't the case in an example.
Feel free to use bad style, but don't teach it as the recommended way.
The semantics of the first are guaranteed. The semantics of the second are undefined in many languages, and LSL isn't documented well enough to know what it means. Really folks, don't do this unless you know what you're doing and why you're doing it, and don't recommend it to others as the default way to append to a list.
|
|
Learjeff Innis
musician & coder
Join date: 27 Nov 2006
Posts: 817
|
03-30-2007 18:41
Well, testing shows that it still saves memory. With a single character string, it's about 8 bytes per list item. However, with longer strings the savings increases, to roughly equal the string length.
The implementation of the += operator for strings has a bug, clearly a memory leak. I might take a look at the client source to see if it can be worked around in the compiler: it shouldn't be hard to generate the byte code for the optimized version when encountering the straightforward one.
Of course, it would be better to interpret the code correctly on the server. The optimized version takes 12 more bytes of code.
Thanks for the tip.
|