Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Counting backwards within a list

Domneth Dingson
Registered User
Join date: 20 Nov 2006
Posts: 126
08-08-2007 12:33
I can make a script count up in a list, pick it apart, etc.. but I'm having a heck of a time getting it to count down. Well, I can make it count down, but when it gets to the beginning, it still keeps responding.


My thoughts were to reorganize the list so it's reversed and then just count up(I can make that code work), but I assume that'd be more work than necessary.

CODE

list listA = ["a","b","c","d","e","f","g"];
integer count;
integer nextcount;
default
{
state_entry()
{
integer count = llGetListLength(listA);
}

touch_start(integer total_number)
{
count = count -1;
nextcount = count;
if(nextcount > -1 || nextcount != 0)
{
llOwnerSay(llList2String(listA,count));
}
else
{
llOwnerSay("end of list");

}
}
}
Qie Niangao
Coin-operated
Join date: 24 May 2006
Posts: 7,138
08-08-2007 12:49
When would
From: someone

if(nextcount > -1 || nextcount != 0)
be false?
Domneth Dingson
Registered User
Join date: 20 Nov 2006
Posts: 126
08-08-2007 13:32
From: Qie Niangao
When would
be false?



Thanks Qie!
Salvador Nakamura
http://www.sl-index.com
Join date: 16 Jan 2007
Posts: 557
08-08-2007 13:36
CODE

list listA = ["a","b","c","d","e","f","g"];
integer count;
integer nextcount;

default
{
state_entry()
{
count = llGetListLength(listA);

}

touch_start(integer total_number)
{
--count;

if(count >= 0)
{
llOwnerSay(llList2String(listA,count));
}
else
{
llOwnerSay("end of list");
// count=llGetListLength(listA);
// llResetScript();
}
}
}


would do the trick
Domneth Dingson
Registered User
Join date: 20 Nov 2006
Posts: 126
08-08-2007 14:13
From: Salvador Nakamura
CODE

list listA = ["a","b","c","d","e","f","g"];
integer count;
integer nextcount;

default
{
state_entry()
{
count = llGetListLength(listA);

}

touch_start(integer total_number)
{
--count;

if(count >= 0)
{
llOwnerSay(llList2String(listA,count));
}
else
{
llOwnerSay("end of list");
// count=llGetListLength(listA);
// llResetScript();
}
}
}


would do the trick



touch_start(integer total_number)
{
count = count -1;
//nextcount = count;
if(count >= -7)
{
llOwnerSay(llList2String(listA,count));
}
else
{
llOwnerSay("end of list";);

}
}
CODE


The above is what I had come up with just before you posted.. is there any difference in our 2 different ways of getting the same solution?
Squirrel Wood
Nuteater. Beware!
Join date: 14 Jun 2006
Posts: 471
08-09-2007 02:23
To optimize your scripting skeelz....

count = count -1

can also be written as:

count -= 1

or just...

count--
Baron Hauptmann
Just Designs / Scripter
Join date: 29 Oct 2005
Posts: 358
08-09-2007 13:39
. . . or just --count, which I'm told gives slightly better performance.
Ed Gobo
ed44's alt
Join date: 20 Jun 2006
Posts: 220
08-09-2007 18:20
Keep it simple!

CODE

touch_start(integer total_number)
{
if ((count--) > 0)
{
llOwnerSay(llList2String(listA,count));
}
else
{
llOwnerSay("end of list");

}
}


or this might be very slightly faster:

CODE

touch_start(integer total_number)
{
if (count > 0)
{
llOwnerSay(llList2String(listA, --count));
}
else
{
llOwnerSay("end of list");

}
}