Join us in Outworldz at www.outworldz.com:9000 or follow us:

Search dozens of selected web sites for OpenSim and LSL script

New! Script Meta-Search will search thousands of scripts here and at other sites for LSL or Opensim scripts.
Loading

Want to add a script or a project? Upload it and a half million people will see it and your name here this year.

Home   Show All
Category: Contributor: Creator
Lists Multidimensional_Array_API  

Multidimensional_Array_API

This script allows you to store data in lists as you would in a C++ or Java multidimensional array. It is a bit more flexible than those language's arrays, since it allows you to dynamically resize each dimension; You don't have to define the size of the array before you use it. If you ever ran into the "Runtime Error: Lists cannot contain lists" message and really needed a list within another, this is what you can use.

For example, say you had a chessboard with each square a different color. Your script can refer to each color using a two-dimensional array; one number for the row another for the column of the particular color.

Category: Lists
By : Christopher Omega
Created: 2010-01-10 Edited: 2010-01-10
Worlds: Second Life

the Zip file

Download all files for Multidimensional_Array_API
Contents are in zip format, with .LSL (text) source code and LSLEdit (text + Solution) formats.
Get file # 1. Multidimensional_Array_API_1.lsl
Get file # 2. Multidimensional_Array_API_2.lsl
1 default {
3 integer SQUARES_ON_SIDE = 12;
4 // Randomly assign colors to each square:
5 list colorArray;
6 integer row;
7 integer col;
8 for (row = 0; row < SQUARES_ON_SIDE; ++row) {
9 for (col = 0; col < SQUARES_ON_SIDE; ++col) {
10 vector color = <llFrand(1), llFrand(1), llFrand(1)>;
11 colorArray = setArray(colorArray, [row, col], [color]);
12 }
13 }
14
15 // Get the color of the square at row 1, column 2:
16 vector squareColor = (vector) llList2String(getArray(colorArray, [0, 1]), 0); // Note that arrays, like LSL's lists, are 0-based; they start counting at 0, not 1.
17 // ...
18
19 // Get the colors of all the squares in row 4:
20 list rowColors = getArray(colorArray, [3]);
21 // ...
22 }
23 }

Multidimensional_Array_API

Getting an element at index [1, 5, 2, 9], in an array "A" translates to:
Get list at index 1 in A, call this B,
Get list at index 5 in B, call this C,
Get list at index 2 in C, call this D,
Return element at index 9 in D.

And here are the functions :)

Category: Lists
By : Christopher Omega
Created: 2010-01-10 Edited: 2010-01-10
Worlds: Second Life

1 // Generates a random string of length len
2 // from the characters passed to it.
3 string randomSeperator(integer len) {
4 integer firstChar = (integer)llFrand(60) + 20; // Range of printable chars = 0x20 to 0x7E
5 if(len <= 1)
6 return llUnescapeURL("%"+(string)firstChar);
7 integer lastChar;
8 do { // Last char must not equal first char.
9 lastChar = (integer)llFrand(60) + 20;
10 } while(lastChar == firstChar);
11 string ret = llUnescapeURL("%"+(string)firstChar);
12 for (len -= 2; len > 0; --len) {
13 integer val;
14 do {
15 val = (integer)llFrand(60) + 20;
16 } while(val == firstChar || val == lastChar);
17 ret += llUnescapeURL("%" + (string)val);
18 }
19 return ret + llUnescapeURL("%"+(string)lastChar);
20 }
21
22 integer SEPERATOR_LEN = 3;
23 string dumpList2String(list src) {
24 // Generate a seperator not present in any of the
25 // elements in the list.
26 string chars = (string) src; // Squashes all elements together.
27 string seperator;
28 do {
29 seperator = randomSeperator(SEPERATOR_LEN);
30 } while(llSubStringIndex(chars, seperator) != -1);
31 return seperator + llDumpList2String(src, seperator);
32 }
33
34 list parseStringKeepNulls(string src) {
35 // The seperator should be the first SEPERATOR_LEN
36 // characters in the string.
37 return llParseStringKeepNulls(llDeleteSubString(src, 0, SEPERATOR_LEN - 1),
38 [llGetSubString(src, 0, SEPERATOR_LEN - 1)], []);
39 }
40
41 list setListElement(list dest, list src, integer index) {
42 if(src == []) {
43 return llListReplaceList(src, [""], index, index);
44 } else {
45 integer len = llGetListLength(dest);
46 for (; len < index; ++len) {
47 dest += "";
48 }
49 return llListReplaceList(dest, src, index, index + llGetListLength(src) - 1);
50 }
51 }
52
53
54 list getArray(list array, list index){
55 if(index == [])
56 return array;
57 integer numIndicies = llGetListLength(index);
58 list src = array;
59 integer i;
60 for (i = 0; i < numIndicies - 1; ++i) {
61 integer listIndex = llList2Integer(index, i);
62 string element = llList2String(src, listIndex);
63 if(llGetSubString(element, 0, 0) == "l")
64 element = llDeleteSubString(element, 0, 0);
65 src = parseStringKeepNulls(element);
66 }
67 string element = llList2String(src, llList2Integer(index, -1));
68 if(llGetSubString(element, 0, 0) == "l") {
69 // Caller is retreiving a dimension.
70 return parseStringKeepNulls(llDeleteSubString(element, 0, 0));
71 } else {
72 return [element];
73 }
74 }
75
76 // To set an element, we need to extract
77 // each list starting from where exactly the
78 // new data will be. In a set of russian dolls, this
79 // would be equivelant to grabbing the littlest one
80 // first. then gradually adding back on the layers over it.
81 // I think its easiest to do this recursively.
82 list setArray(list array, list index, list data) {
83 string element;
84 if(llGetListLength(data) > 1) {
85 // Data is a new dimension:
86 element = "l" + dumpList2String(data);
87 } else {
88 // Data is a single element
89 element = llList2String(data, 0);
90 }
91 if(llGetListLength(index) > 1) {
92 // index is in the form [a,b,c,d]
93 // here, we grab the list that d is in.
94 list containerIndex = llDeleteSubList(index, -1, -1);
95 list dest = getArray(array, containerIndex);
96 integer listIndex = llList2Integer(index, -1); // Grab d
97 dest = setListElement(dest, [element], listIndex); // replace the element in d's list.
98 // Make sure the recursion treats the container like a list, not an element.
99 if(llGetListLength(dest) == 1)
100 dest += "";
101 return setArray(array, containerIndex, dest);
102 } else {
103 return setListElement(array, [element], llList2Integer(index, 0));
104 }
105 }

Back to the Best Free Tools in Second Life and OpenSim.