Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Strange Loop Behavior...

Cypher Ragu
[Mad Scientist]
Join date: 6 Jul 2008
Posts: 174
04-03-2009 17:07
...I have a rather large script, used to help organize landmarks.

If a landmark is found in the object's inventory that hasn't been properly added, I want the script to remove that landmark automatically to help prevent problems later on.

Here's what I have so far:

From: someone

llOwnerSay("Number of landmarks: " + (string)llGetInventoryNumber(INVENTORY_LANDMARK)); //Debug
integer i = 0;
while (i < llGetInventoryNumber(INVENTORY_LANDMARK)) //Same as saying "while (i <= llGetInventoryNumber(INVENTORY_LANDMARK) - 1)"//
{
llOwnerSay("Index: " + (string)i); //Debug
llOwnerSay("Name: " + llGetInventoryName(INVENTORY_LANDMARK, i)); //Debug
if (llListFindList(InventoryLandmarks, [llGetInventoryName(INVENTORY_LANDMARK, i)]) == -1)
{
llOwnerSay("TRUE";); //More debug
if (llGetInventoryName(INVENTORY_LANDMARK, i) != "";) llRemoveInventory(llGetInventoryName(INVENTORY_LANDMARK, i));
}
i++;
llOwnerSay("New index: " + (string)i); //Still more debug
}
llOwnerSay("Loop exit";);



The trouble is that the loop is ending too early. I added a bunch of debug in there, but it just doesn't make sense.

Say I add 6 landmarks that haven't been "Accounted for." The script picks up the number of landmarks as 6, which is perfectly correct. But, for some reason, the loop thinks that "integer i" is equal to/greater than 6, even when it's only up to 4.

(I just used 4 as an example. It's usually some completely random number.)


So this is what all the debug might come up to:

From: someone

Number of landmarks: 6
Index: 0
Name: Landmark0
TRUE
New index: 1

Index: 1
Name: Landmark1
TRUE
New index: 2

Index: 2
Name: Landmark2
TRUE
New index: 3

Index: 3
Name: Landmark3
TRUE
New Index: 4

Loop exit


I'm really stumped. :confused: Any help is appreciated. Thanks! ^_^
_____________________
Life is a highway... And I just missed my exit.
Papalopulus Kobolowski
working mind
Join date: 11 Aug 2006
Posts: 326
04-03-2009 19:28
I add some things to test it, but its work for me

CODE



list InventoryLandmarks;
integer l;
integer index;

check_add_l()
{




l=llGetInventoryNumber(INVENTORY_LANDMARK);
index = 0;
while (index < l) //Same as saying "while (i <= llGetInventoryNumber(INVENTORY_LANDMARK) - 1)"//
{


if (llListFindList(InventoryLandmarks, [llGetInventoryName(INVENTORY_LANDMARK, index)]) == -1)
{


if (llGetInventoryName(INVENTORY_LANDMARK, index) != "") llRemoveInventory(llGetInventoryName(INVENTORY_LANDMARK, index));
llOwnerSay("Landmark Removed");
}
index++;
llOwnerSay((string)index);

}
llOwnerSay("Loop exit");


}

default
{
state_entry()
{
llOwnerSay("ready to run!!");

}

touch_start(integer total_number)
{check_add_l();

}
}
_____________________


RAW terrain files - terraform your SIM!!
http://www.wishland.info/
PD:the wiki its your friend ;)
http://wiki.secondlife.com/wiki/LSL_Portal
Cypher Ragu
[Mad Scientist]
Join date: 6 Jul 2008
Posts: 174
04-03-2009 19:53
Weird. I'll try recompiling.
_____________________
Life is a highway... And I just missed my exit.
Kaluura Boa
Polygon Project
Join date: 27 Mar 2007
Posts: 194
04-04-2009 01:55
This isn't strange, this is perfectly normal. It's just a typical case of "removing the rug under your feet".

You are using a growing index to walk a list and you shorten this list along the way. Sooner or later your index will point beyond the end of the list.

Let's take a short list:

list: ["A", "B"]
index: 0
content: "A"

We delete this item and increment the index:

list: ["B"]
index: 1
content: ?????

So... Always walk your inventory from end to start when you delete items in it.

integer i = llGetInventoryNumber(INVENTORY_LANDMARK) - 1;
while (i >= 0)
{
string name = llGetInventoryName(INVENTORY_LANDMARK, i);
if (llListFindList(InventoryLandmarks, [name]) == -1)
{
if (name != "";) { llRemoveInventory(name); }
}
--i;
}
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
04-04-2009 08:15
From: Kaluura Boa
So... Always walk your inventory from end to start when you delete items in it.

or decrement your max count and not your current count (that's really bad form, but sometimes it's necessary)
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -
Cypher Ragu
[Mad Scientist]
Join date: 6 Jul 2008
Posts: 174
04-06-2009 10:59
Ah, of course. What a stupid mistake.
_____________________
Life is a highway... And I just missed my exit.