Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Can I assign values to a list of variables?

Chaz Longstaff
Registered User
Join date: 11 Oct 2006
Posts: 685
01-15-2008 22:32
I know how to do this in a long texty way, but was wondering if there is a more concise way.

Say the following:
list myvariables = [Variable_01,Variable_02, Variable_03, Variable_04, Variable_05];
list incomingvalues = ["red", "brown", "green", "blue"];

Is there any way to go something like:

integer length = llGetListLength(myvariables);
for (x = 0; x < length; x++) {
myvariables[x] = llList2String(incomingvalues, x);

}
Deanna Trollop
BZ Enterprises
Join date: 30 Jan 2006
Posts: 671
01-15-2008 23:05
From: Chaz Longstaff
list myvariables = [Variable_01,Variable_02, Variable_03, Variable_04, Variable_05];

What datatype(s) are Variable_01, Variable_02, etc? Do you want the string "Variable_01", or is Variable_01 a previously-declared variable? If the latter, you will not be able to assign in global declaration, since only literal value assignment is possible there. You could assign in default state_entry(), though. However, that will only assign the *value* of Variable_01 (etc.) to the list element, not a pointer or reference, neither of which exist in LSL.

e.g.:

string Variable_01 = "foo";
key Variable_02 = "bar";
integer Variable_03 = 5318008;
float Variable_04 = 2.54;
vector Variable_05 = < 1.23, 4.56, 7.89 >;

list myvariables;

default
{
state_entry()
{
myvariables = [Variable_01,Variable_02, Variable_03, Variable_04, Variable_05];
}
}

Then, myvariables would have the value [ "foo", (key)"bar", 5318008, 2.54, < 1.23, 4.56, 7.89 > ].

From: someone
myvariables[x] = llList2String(incomingvalues, x);
There is no subscript operator in LSL. All list manipulation, beyond verbatim copying of one list to another (which includes nullification), must be done via the list manipulation functions.
Beezle Warburton
=o.O=
Join date: 10 Nov 2006
Posts: 1,169
01-15-2008 23:12
From: Chaz Longstaff
I know how to do this in a long texty way, but was wondering if there is a more concise way.

Say the following:
list myvariables = [Variable_01,Variable_02, Variable_03, Variable_04, Variable_05];
list incomingvalues = ["red", "brown", "green", "blue"];

Is there any way to go something like:

integer length = llGetListLength(myvariables);
for (x = 0; x < length; x++) {
myvariables[x] = llList2String(incomingvalues, x);

}


I'm assuming you're trying to match up a set of vector color values with a list of names.

To the best of my knowledge, LSL doesn't use arrays at all, so you're stuck using "parallel lists"

Simple color changer example:

From: someone

list colornames = ["red","green","blue"];
list colorvalues = [<1,0,0>,<0,1,0>,<0,0,1>];
integer dialogChannel = 2;

integer handler;
default
{
on_rez(integer P)
{
llResetScript();
}
state_entry()
{
llListenRemove(handler);
handler = llListen( dialogChannel, "", llGetOwner(), "" );
}
listen( integer channel, string name, key id, string message )
{
if(llListFindList(colornames,[message]) != -1)
{
// pointer is the location in the list
// which is then used to call the second list.
integer pointer = llListFindList(colornames,[message]);
llSetColor(llList2Vector(colorvalues,pointer),ALL_SIDES);
}
}
touch_start(integer total_number)
{
llDialog(llGetOwner(), "Color Menu", colornames, dialogChannel);
}
}
_____________________
Though this be madness, yet there is method in't.
-- William Shakespeare

Warburton's Whimsies:
In SL
Apez.biz
Squirrel Wood
Nuteater. Beware!
Join date: 14 Jun 2006
Posts: 471
01-15-2008 23:48
May I suggest using *strided* lists instead of parallel lists?
Chaz Longstaff
Registered User
Join date: 11 Oct 2006
Posts: 685
01-16-2008 00:02
>> What datatype(s) are Variable_01, Variable_02, etc? Do you want the string "Variable_01", or is Variable_01 a previously-declared variable?

Previously declared. To expand:

//variables
string Variable_01;
string Variable_02;
string Variable_03;
string Variable_04;

list myvariables = [Variable_01,Variable_02, Variable_03, Variable_04, Variable_05];

default {

link_message(integer sender_num, integer num, string str, key id){
list incomingvalues = llCSV2List(str);
// list incomingvalues = ["red", "brown", "green", "blue"];

integer length = llGetListLength(myvariables);
for (x = 0; x < length; x++) {
myvariables[x] = llList2String(incomingvalues, x);
}

}


}
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
01-16-2008 00:43
Note that everything is by value in LSL, not reference. So the initial value of your 'myvariables' list is actually going to be taken from the values of your variables at the point of your list assignment. Then when you later change your list, the individual variables you used to first initialize the list will NOT change in value.

So long as you understand that, we can talk about the last bit. Lists also cannot be changed. All you can do is construct a new list to assign to your list variable. You can create a new list composed of the elements of an old list with some of the elements replaced using llListReplaceList(). This is often used to "re-assign" an element of a list. For example:

CODE

list myList;
...
myList = llListReplaceList(myList, [ newValue ], index, index);


Note that that assignment back to myList is essential, since lists are not modifiable in place. There are also functions to extract a sub-list of an existing list. The FOR loop in your example could be replaced with the line:

CODE

myvariables = llList2List(incomingvalues, 0, length);


Please see the table of functions in http://www.lslwiki.net/lslwiki/wakka.php?wakka=list
Chaz Longstaff
Registered User
Join date: 11 Oct 2006
Posts: 685
01-16-2008 00:55
ah kk

thanks everybody. I just wondered if I were missing an obvious, less texty way to save memory in a script. Guess not.

Thanks, will just keep doing it written out the "longhand" way, as it were.