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
Arrays MultiDimensional_Arrays_in_LSL  

MultiDimensional_Arrays_in_LSL

MultiDimensional_Arrays_in_LSL

Category: Arrays
By : Jamethiel Wickentower
Created: 2010-01-10 Edited: 2010-01-10
Worlds: Second Life

the Zip file

Download all files for MultiDimensional_Arrays_in_LSL
Contents are in zip format, with .LSL (text) source code and LSLEdit (text + Solution) formats.
Get file # 1. MultiDimensional_Arrays_in_LSL_1.lsl
1 // Copyright 2008 Jamethiel Wickentower (David Lloyd)
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 // #########################################################################
15
16 // This library implements single dimensional and multi-dimensional arrays.
17 //
18 // Currently implemented:
19 //
20 // * Searching for a value
21 //
22 // To be implemented:
23 //
24 // * Removing a single value or a whole row
25 // * Replacing a single value or a whole row
26 // * Inserting a single value or a whole row
27 //
28 // The last operation may not make sense with arrays, though.
29 //
30 // This does not handle hash maps.
31
32 key gOwner;
33 string gOwnerName;
34
35 list testList = [
36 1, "DSL-",
37 "DSL-1", "first", "row", "now",
38 "DSL-2", "onto", "the", "chocolate", "boards", "we",
39 "DSL-3", "go", "west"];
40
41 list fakeList = [
42 1, "DSL-"];
43
44 list test2Dims = [
45 2, "DSL-", "LSD-",
46 "DSL-1",
47 "LSD-1", "father", "christmas",
48 "LSD-2", "easter", "bunny",
49 "LSD-3", "saint", "nicholas"];
50
51 list test2Complex = [
52 "DSL-1",
53 "LSD-1", "David", "Lloyd",
54 "LSD-2", "Janet", "Barker",
55 "DSL-2",
56 "LSD-1", "None",
57 "DSL-3",
58 "LSD-1", "Tony",
59 "LSD-2", "Barber",
60 "LSD-3", "Shindig", "Nonce"];
61
62 report(string msg) {
63 llOwnerSay(msg);
64 }
65
66 // jw_auto_array_get
67 //
68 // This reads a special array that is setup like this:
69 //
70 // item[0] = number of dimensions
71 // item[0..N] = prefixes
72 //
73 // The prefixes MUST be unique and contained within the data or bad things
74 // will certain happen.
75 //
76 // You can search a one dimensional array by giving a single number in the
77 // needle list.
78 list jw_auto_array_get(list haystack, list needle) {
79 integer where;
80
81 integer haystack_len = llGetListLength(haystack);
82 integer needle_len = llGetListLength(needle);
83
84 if(haystack_len < 3) {
85 report("Invalid or empty list.");
86 return [];
87 }
88
89 integer dimensions = llList2Integer(haystack, 0);
90
91 if(dimensions != needle_len) {
92 report("Invalid haystack length.");
93 return [];
94 }
95
96 list prefixes = llList2List(haystack, 1, dimensions);
97
98 list result = jw_array_recursive(haystack, prefixes, needle, 0);
99 return result;
100 }
101
102 // jw_custom_array_get
103 //
104 // Similar to jw_auto_array_get but the prefix information is given via a
105 // parameter. See the discussion above.
106 list jw_custom_array_get(list haystack, integer dimensions, list prefixes, list needle) {
107 integer where;
108
109 integer haysteck_len = llGetListLength(haystack);
110 integer needle_len = llGetListLength(needle);
111
112 if(dimensions != needle_len) {
113 report("Invalid haystack length.");
114 return [];
115 }
116
117 list result = jw_array_recursive(haystack, prefixes, needle, 0);
118 return result;
119 }
120
121 // jw_get_multi_dim
122 //
123 // A recursive function that gets a particular multi-dimensional array. It calls itself
124 // on itself until it gets down to the requisite level and then returns everything in
125 // that level.
126 //
127 // That could be an array itself and, in fact, it is possible I could automate that
128 // process.
129 list jw_array_recursive(list haystack, list prefixes, list needle, integer count) {
130 integer prefixes_len = llGetListLength(prefixes);
131
132 if(count >= prefixes_len) {
133 return haystack;
134 }
135
136 string what = llList2String(prefixes, count) + llList2String(needle, count);
137 string expected = llList2String(prefixes, count) + (string)(llList2Integer(needle, count) + 1);
138
139 haystack = jw_get_row(haystack, what, expected);
140
141 // If we can't find a particular entry, we keep on returning
142 if(haystack == [ ]) {
143 return haystack;
144 }
145
146 count++;
147 list result = jw_array_recursive(haystack, prefixes, needle, count);
148 return result;
149 }
150
151 // Get Row
152 //
153 // Gets a particular row from the array; this can be used recursively to drill
154 // into multi-level arrays.
155 list jw_get_row(list haystack, string what, string expected) {
156 list result;
157
158 integer haystack_len = llGetListLength(haystack);
159 integer where = llListFindList(haystack, [ what ]);
160
161 // Couldn't find the first dimension...
162 if(where < 0) {
163 report("Unable to find " + what);
164 return result;
165 }
166
167 integer i;
168 for (i = where + 1; i < haystack_len; i++) {
169 string item = llList2String(haystack, i);
170 if(item == expected) {
171 jump finished;
172 }
173 result += [ item ];
174 }
175
176 @finished;
177
178 return result;
179 }
180
181 default
182 {
184 {
185 gOwner = llGetOwner();
186 gOwnerName = llKey2Name(gOwner);
187
188 llInstantMessage(gOwner, "Reset test list script...");
189 }
190
191 touch_start(integer total_number)
192 {
193 llOwnerSay("We found these:\n" + llDumpList2String(jw_auto_array_get(test2Dims,[ 1, 3 ]), "\n"));
194 llOwnerSay("Complex, we found these:\n" + llDumpList2String(jw_custom_array_get(test2Complex, 2, [ "DSL-", "LSD-" ], [ 3, 3 ]), "\n"));
195 }
196 }

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