[lp_array]

Description

Link: [lp_array]
Author: Bil Corry
Category: Array
Version: 8.x
License: Public Domain
Posted: Nov. 22, 2005
Updated: Oct. 30, 2006
More by this author...
This is a replacement for [array].  I extended [array] to have some features of map (I like having items in order and sortable, but like the way you can store and search for values within a map.  This is a bridge between the two).  It adds other member tags to the array type, like ->random and ->roundrobin.  The latter is great for creating alternating rows within a table, just assign it two or more colors and cycle through them in your looping container.

[lp_array] has the following addtional member tags:

setindex - sets the pointer for roundrobin and reverserobin
->(setindex: 5)
->(setindex: 'first')
->(setindex:'last')


getindex - gets the pointer for roundrobin and reverserobin
->(getindex)

roundrobin - returns the next item in the array, wraps to the first element after the last element.
->(roundrobin)

reverserobin - returns the previous item in the array, wraps to the last element before the first element
->(reverserobin)

random - returns a random element from the array
->(random)

popfirst - returns the first element from the array and removes it from the array
->(popfirst)

poplast - returns the last element from the array and removes it from the array
->(poplast)

poprandom - returns a random element from the array and removes it from the array
->(poprandom)

pushfirst - inserts an element into the array at the beginning of the array
->(pushfirst: 'abc')

pushlast - inserts an element into the array at the end of the array
->(pushlast:'xyz')

pushrandom - inserts the element into a random place within the array
->(pushrandom:'lmn')

join - similar to ->join
    params:
       join - what to join the elements with
       front - what to prepend to the joined string
       back - what to append to the joined string
       first - what to join the first two elements with
       last - what to join the last two elements with
->(join: ', ',-last=' and ')


keys - works like map->keys when the array contains pairs
->(keys)

values - returns the values for all pairs within the array
->(values)

removepair - removes a pair element given a key
->(removepair:'car')

insertpair - inserts a pair element, works like map->insert by overwriting any existing pair with the same key
->(insertpair: (pair: 'car' = 'edsel'))

findpair - works like map->find, returns the value given a key
->(findpair: 'car')

findvalue - same as findpair
->(findvalue:'car')

findkeys - returns all keys from pairs containing the value given
->(findkeys:'edsel')

Parameters

none


Sample Usage

var:'colors' = (lp_array: 'red','green','blue','yellow','orange','pink');
var:'color' = string;
var:'loop' = 10;

'$colors = ('($colors->(join:','))')';

'<br><h1>Roundrobin Demo</h1>';
$colors->(setindex: 1);
loop: $loop;
	$color = ($colors->roundrobin);
	'<font color="' $color '">' loop_count ': ' $color '</font><br>';
/loop;


'<br><h1>Reverserobin Demo</h1>';
$colors->(setindex: 'last');
loop: $loop;
	$color = ($colors->reverserobin);
	'<font color="' $color '">' loop_count ': ' $color '</font><br>';
/loop;


'<br><h1>Random Demo</h1>';
loop: $loop;
	$color = ($colors->random);
	'<font color="' $color '">' loop_count ': ' $color '</font><br>';
/loop;


'<br><h1>Map-Features Demo</h1>';

var:'contact' = (lp_array: 'first' = 'Jane', 'last' = 'Doe', 'phone' = '3335551212', 'email' = 'janedoe@janedoeland.com', 'maiden_name' = 'Doe');

'$contact = ' $contact '<br><br>';
'(notice the order is kept!)<br><br>';
'->keys = ' $contact->keys '<br>';
'->values = ' $contact->values '<br>';
$contact->(insertpair:'phone' = '5553331111')
'->insertpair (overwrites value, doesn\'t add new element to array):(phone=\'5553331111\') = ' $contact->(find:'phone')->(get:1) '<br>';
'->findpair:\'email\' = ' ($contact->(findpair:'email')) '<br>';
'->findkeys:\'Doe\' = ' $contact->(findkeys:'doe') '<br>';
						

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
[

define_type:'lp_array', 'array',
	-description='[array] with additional member tags.';
	local:'index' = 1;

	define_tag:'setindex',
		-required='index';
		if:(local:'index') == 'first';
			self->'index' = 1;
		else: (local:'index') == 'last';
			self->'index' = self->size;
		else: (integer: (local:'index')) < 1;
			fail: -1, 'Index value is too small.';
		else: (integer: (local:'index')) > self->size;
			fail: -1, 'Index value is too large.';		
		else;
			self->'index' = (integer: #index);
		/if;
	/define_tag;

	define_tag:'getindex';
		return: self->'index';
	/define_tag;

	define_tag:'roundrobin';
		if: self->size == 0;
			return: null;
		/if;
		if: self->'index' > self->size;
			self->'index' = self->size;
		/if;
		local:'temp' = self->(get: self->'index');
		self->'index' += 1;
		if: self->'index' > self->size;
			self->'index' = 1;
		/if;
		return: #temp;
	/define_tag;

	define_tag:'reverserobin';
		if: self->size == 0;
			return: null;
		/if;
		if: self->'index' > self->size;
			self->'index' = self->size;
		/if;
		local:'temp' = self->(get: self->'index');
		self->'index' -= 1;
		if: self->'index' < 1;
			self->'index' = self->size;
		/if;
		return: #temp;
	/define_tag;

	define_tag:'random';
		if: self->size == 0;
			return: null;
		/if;
		self->'index' = (math_random: -lower=1, -upper=self->size);
		return: self->(get: self->'index');
	/define_tag;

	define_tag:'popfirst';
		if: self->size == 0;
			fail: -1, 'Empty array.';
		/if;		
		local:'temp' = self->(get: 1);
		self->(remove: 1);
		return: #temp;
	/define_tag;

	define_tag:'poplast';
		if: self->size == 0;
			fail: -1, 'Empty array.';
		/if;		
		local:'temp' = self->(get: self->size);
		self->(remove: self->size);
		return: #temp;
	/define_tag;

	define_tag:'poprandom';
		if: self->size == 0;
			fail: -1, 'Empty array.';
		/if;
		local:'random' = (math_random: -lower=1, -upper=self->size);
		local:'temp' = self->(get: #random);
		self->(remove: #random);
		return: #temp;		
	/define_tag;

	define_tag:'pushfirst',-required='item';
		self->(insertfirst: #item);
	/define_tag;
	
	define_tag:'pushlast',-required='item';
		self->(insertlast: #item);
	/define_tag;

	define_tag:'pushrandom',-required='item';
		// this is how we would do it if ctypes with parents worked properly
		// self->(insert: #item, (math_random: -lower=1, -upper=self->size + 1));
		//
		// but it doesn't, so instead, we have to do the following...
		
		// save error so we won't overwrite it with our protect below
		local:'_ec' = error_code;
		local:'_em' = error_msg;
		lp_error_clearError;

		if: self->size == 0;
			self->(insert: #item);
		else;
			local:'random' = (math_random: -lower=1, -upper=self->size + 1);
			local:'temp' = array;
			loop: self->size + 1;
				if: loop_count == #random;
					#temp->(insert: #item);
				/if;

				protect;
					#temp->(insert: self->popfirst);
				/protect;
			/loop;
			iterate: #temp, local:'t';
				self->(insert: #t);
			/iterate;
		/if;

		// restore the error, if any
		error_code = #_ec;
		error_msg  = #_em;
	/define_tag;


	define_tag:'join',
		-optional='join',
		-optional='front',
		-optional='back',
		-optional='first',
		-optional='last';
		if: self->size == 0;
			return: null;
		/if;
		if: !local_defined:'join';
			local:'join' = '';
		/if;
		if: !local_defined:'front';
			local:'front' = '';
		/if;
		if: !local_defined:'back';
			local:'back' = '';
		/if;
		if: !local_defined:'first';
			local:'first' = #join;
		/if;
		if: !local_defined:'last';
			local:'last' = #join;
		/if;
			if: self->size == 1;
			return: #front (self->(get: 1)) #back;
		/if;
		if: self->size == 2;
			if: #last->size;
				return: #front (self->(get:1)) #last (self->(get:2)) #back;
			else: #first->size;
				return: #front (self->(get:1)) #first (self->(get:2)) #back;
			else;
				return: #front (self->(get:1)) #join (self->(get:2)) #back;
			/if;
		/if;
		local:'return' = #front;
		#return += self->(get:1);
		#return += #first;
		loop: -from=2, -to=self->size - 1;
			#return += self->(get:loop_count);
			#return += #join;
		/loop;
		#return->(removetrailing: #join);
		#return += #last;
		#return += self->(get:self->size);
		#return += #back;
		return: #return;
	/define_tag;

	define_tag:'keys';
		local:'keys' = array;
		iterate: self, local:'s';
			if: #s->type == 'pair';
				#keys->(insert: #s->name);
			/if;
		/iterate;
		return: #keys;
	/define_tag;

	define_tag:'values';
		local:'values' = array;
		iterate: self, local:'s';
			if: #s->type == 'pair';
				#values->(insert: #s->value);
			/if;
		/iterate;
		return: #values;
	/define_tag;

	define_tag:'removepair', -required='key';
		local:'pos' = self->(findposition: #key);
		while: #pos->size > 0;
			self->(remove: #pos->(get:1));
			#pos = self->(findposition: #key);
		/while;
	/define_tag;

	define_tag:'insertpair', -required='pair';
		if: #pair->type != 'pair';
			self->(removepair: #pair);
			self->(insert: (pair: #pair = null));
		else;
			self->(removepair: #pair->name);
			self->(insert: #pair);
		/if;
	/define_tag;

	define_tag:'findpair', -required='key';
		local:'findpair' = self->(findposition: #key);
		if: #findpair->size == 0;
			return: null;
		/if;
		#findpair = self->(get: #findpair->(get:1));
		if: #findpair->type == 'pair';
			return: #findpair->value;
		else;
			return: #findpair;
		/if;
	/define_tag;

	define_tag:'findvalue', -required='key'; // same as findpair
		local:'findpair' = self->(findposition: #key);
		if: #findpair->size == 0;
			return: null;
		/if;
		#findpair = self->(get: #findpair->(get:1));
		if: #findpair->type == 'pair';
			return: #findpair->value;
		else;
			return: #findpair;
		/if;
	/define_tag;

	define_tag:'findkeys', -required='value';
		local:'keys' = array;
		iterate: self, local:'s';
			if: #s->type == 'pair' && #s->value == #value;
				#keys->(insert: #s->name);
			/if;
		/iterate;
		return: #keys;
	/define_tag;

/define_type;

]

 

Related Tags



Comments

none

Email:


Password:



Newest

Most Popular