Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Changing values in a list

BrunoOlsen Oh
Registered User
Join date: 30 Mar 2007
Posts: 38
04-30-2007 05:22
I want to change a value in a list at a spicific index chosen by different circumstances. The index is in a var, and that is not the problem, the problem is how to change the value. How do I do that?
Deanna Trollop
BZ Enterprises
Join date: 30 Jan 2006
Posts: 671
04-30-2007 05:49
llListReplaceList
BrunoOlsen Oh
Registered User
Join date: 30 Mar 2007
Posts: 38
04-30-2007 07:37
is that the only way?
Kenn Nilsson
AeonVox
Join date: 24 May 2005
Posts: 897
04-30-2007 09:36
It's by far the best way.
_____________________
--AeonVox--

Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms chasing ghosts, eating magic pills, and listening to repetitive, addictive, electronic music.
BrunoOlsen Oh
Registered User
Join date: 30 Mar 2007
Posts: 38
04-30-2007 10:19
hmm, I risk truncating...
Destiny Niles
Registered User
Join date: 23 Aug 2006
Posts: 949
04-30-2007 10:37
To replace one value you would use a list with just one entry.
ie.

CODE

default
{
touch_start(integer total_number)
{
integer ind = 6
list names = ["Able", "Bob", "Charles", "Dillian", "Ethal", "Frank", "Gigi"];
list newname = ["George"];
foo = llListReplaceList(names, newname, ind, ind); // replace Gigi with George
llOwnerSay(llList2CSV(foo));
}
}
BrunoOlsen Oh
Registered User
Join date: 30 Mar 2007
Posts: 38
04-30-2007 13:23
From: Destiny Niles
To replace one value you would use a list with just one entry.
ie.

CODE

default
{
touch_start(integer total_number)
{
integer ind = 6
list names = ["Able", "Bob", "Charles", "Dillian", "Ethal", "Frank", "Gigi"];
list newname = ["George"];
foo = llListReplaceList(names, newname, ind, ind); // replace Gigi with George
llOwnerSay(llList2CSV(foo));
}
}


So, if I'm updating a value I should do something like (say, I'm changing Charles):

CODE


integer indextochange;
default
{
state_entry(){
indextochange = 3;//charles
}
touch_start(integer total_number)
{
integer ind = indextochange;
list names = ["Able", "Bob", "Charles", "Dillian", "Ethal", "Frank", "Gigi"];
list newname = llParseString2List(nameoptainedelsewhere ,[""],[]); //convert a string to list
integer nameslength = llGetListLength(names);
foo = llListReplaceList(names, newname, ind, nameslength);
names = foo;
llOwnerSay("Names list after change\n" + llList2CSV(names));
}
}
Deanna Trollop
BZ Enterprises
Join date: 30 Jan 2006
Posts: 671
04-30-2007 14:18
From: BrunoOlsen Oh
So, if I'm updating a value I should do something like (say, I'm changing Charles):
Not quite. A few notes:
  1. "Charles" is element 2 in that list (the first element is 0)
  2. llParseString2List isn't necessary in this context. That's for breaking up a string containing elements separated by a specific character or string of characters into a list. To "convert" a variable or literal value to a list, just enclose it in square brackets, e.g. [ SomeFloat ] or [ "Billy" ]
  3. You don't need to know the length of names for this operation. To replace one element with something else, you only need llListReplaceList( list, [ NewElement ], OldIndex, OldIndex ); What you have above will replace everything from "Dillian" onward with nameoptainedelsewhere. In fact (and this is a trivial distinction) this will include one past the end of the list, since you're using the length as the end index, when the last index in a list is always length minus 1. (Again, indexes start at 0)
  4. Assigning the return of llListReplaceList to foo, then assigning foo to names isn't necessary. Just assign to names directly.
BrunoOlsen Oh
Registered User
Join date: 30 Mar 2007
Posts: 38
05-02-2007 10:00
aha. Ok, thanks :) Dealing with lists is a bit different than dealing with arrays, that's why this whole "replace-thing" threw me off ;)
AnnMarie Otoole
Addicted scripter
Join date: 6 Jan 2007
Posts: 162
05-02-2007 20:14
The problem with the llListModifyList is you have to modify it with another list, even if only a single element list and the list name cannot be a variable.

I wrote a custom function I've been using for some time that replaces an entry in a list with a new variable of any type.

CODE

//This function changes the list entry in WhichList[WhichEntry] to NewValue.
//It is similar to llListReplaceList but replaces with a single
// variable instead of a list.
//USE IT THIS WAY:-
//WhichList = (WhichList = []) + ModifyList(WhichList, EntryNumber, (string) NewValue);

list ModifyList(list WhichList, integer WhichEntry, string NewValue) {
list ToList = []; //Declare temporary list to receive elements
string Element; //Local variable to hold list elements
integer ListLength = llGetListLength(WhichList);
integer i;
for (i = 0; i < ListLength; i++) {
Element = llList2String(WhichList,i);
if (i == WhichEntry) Element = NewValue; //Substitue new Value for this one
ToList = (ToList=[]) + ToList + Element; //insert in new list
}
return ToList; // Replace original list with new one
}
Talarus Luan
Ancient Archaean Dragon
Join date: 18 Mar 2006
Posts: 4,831
05-02-2007 20:30
I'm not sure I see the advantage in using your function over llListReplaceList.

It doesn't use less memory (more, actually), and it definitely won't run nearly as fast as the built-in function.
Yumi Murakami
DoIt!AttachTheEarOfACat!
Join date: 27 Sep 2005
Posts: 6,860
05-02-2007 21:16
From: AnnMarie Otoole
The problem with the llListModifyList is you have to modify it with another list, even if only a single element list and the list name cannot be a variable.


It's perfectly legal to specify [x] as the src parameter of llListModifyList if you want to replace a single item in the list with the contents of variable x.

The real problem with llListModifyList is that it makes a copy of the list during the modification process, thus requiring twice as much memory as it otherwise would :(