|
White Hyacinth
Registered User
Join date: 15 Nov 2006
Posts: 353
|
03-31-2007 13:45
Hi scripters, I have little experience with lists but I want to use them to make a FIFO queue. So I want to make a list of strings and lets the first one that came in go out last. I want to have 5 items in the queue. This is what I came up with:
list current = ["", "", "", "", ""];
...blabla...
listen(integer channel, string name, key id, string message) { // echo the message we recieved llSay(0, message);
// tell about the eldest element now to be deleted from the queue llSay(0, "Out:"+ llList2String(current, 4));
// add the new item to the queue current = (list) message + current;
// delete the eldest element current = llDeleteSubList(current, 4, 1); }
But it does not work: The new texts are not added to the queue and the queue gets one element shorter every time this event handler is called. Can anyone please help me?
|
|
Sys Slade
Registered User
Join date: 15 Feb 2007
Posts: 626
|
03-31-2007 14:00
The oldest element in a list will always be the lowest (0), unless you reorder the list every time. When you add to a list, the item goes on the top and gets the highest number. llDeleteSubList takes the start and end integers, not start and length. If end is smaller than start, it acts as an exclusion instead, so 4,1 actually returns items 2 and 3. I would also change the order around so you delete the oldest element first, and then add the new one. Might be a tiny amount, but it should save memory by having a smaller list. So: // echo the message we recieved llSay(0, message);
// tell about the eldest element now to be deleted from the queue llSay(0, "Out:"+ llList2String(current, 0));
// delete the eldest element current = llDeleteSubList(current, 0, 0);
// add the new item to the queue current += [message];
Not in world to test this, but should work.
|
|
White Hyacinth
Registered User
Join date: 15 Nov 2006
Posts: 353
|
03-31-2007 16:32
I have just tried it and YES it works. Thank you very much Sys!
|