Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Combining List Elements

Debra Himmel
Registered User
Join date: 14 Jun 2008
Posts: 226
07-19-2008 02:55
Hello,

After hours of testing I'm still unable to find a way of combining elements into a string in the format I want. If I have a list as:

list MyList = ["One", "No. 1", "Two", "No. 2", "Three", "No. 3"];

How do I create a string that looks like:

string MyString = "One No. 1\nTwo No. 2\nThree No. 3\n";

I am stuck because I can't see a way of accessing the elements in the list I want to access.

Many thanks in advance.
Kaluura Boa
Polygon Project
Join date: 27 Mar 2007
Posts: 194
07-19-2008 06:41
Huuu? What the problem? There are a lot of instructions to extract and manipulate elements from a list.

http://wiki.secondlife.com/wiki/Category:LSL_List

For your little problem:

list MyList = ["One", "No. 1", "Two", "No. 2", "Three", "No. 3"];

string MyString = "";
integer i;
for (i = 0; i <=4 ; i += 2) // In other words: 0, 2, 4
{
MyString += llDumpList2String(llList2List(MyList, i, i + 1), " ";) + "\n";
}

That's all.

llList2List() takes a part of a list. Here, just 2 elements at index i and i+1.

llDumpList2String() builds a string from a whole list, adding any separator you want in between the elements. Here, we take that 2-element list we obtain with llList2List() and we use a blankspace as separator.

We add a new line character at the end and we loop over the next 2 elements.

Piece of cake.
Debra Himmel
Registered User
Join date: 14 Jun 2008
Posts: 226
07-19-2008 08:33
In my own words to see that I have understood the technique, what each loop does is to:

1. take the two elements from the list MyList and create a new (unnamed) list with llList2List.
2. take that new one element list and turn it into a string with llDumpList2String, adding the newline at the end.

This would then also mean that the line:

MyString += (string) llList2List(MyList, i, i) + " " + (string) llList2List(MyList, i + 1, i + 1) + "\n";

also works.

Thank you again.
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
07-19-2008 11:26
From: Debra Himmel
This would then also mean that the line:

MyString += (string) llList2List(MyList, i, i) + " " + (string) llList2List(MyList, i + 1, i + 1) + "\n";

also works.

Well yes, it does, but you are creating new lists out of single string elements just to cast them back to strings. That's not necessary and will be inefficient in terms of memory and performance. You might as well just do:

MyString += llList2String(MyList, i)+" "+llList2String(MyList, i+1)+"\n";