[lp_array_columns]

Description

Link: [lp_array_columns]
Author: Bil Corry
Category: Array
Version: 8.x
License: Public Domain
Posted: Nov. 22, 2005
Updated: Jun. 05, 2006
More by this author...
It takes an array of elements and creates an array of arrays, with each element occuping one cell.  It's used to format a series of elements (numbers, strings, pairs, arrays, etc) into multiple columns/rows.

It will create an even number of columns for each row, filling in the emtpy cell(s) with null.  Rather than testing for null when displaying values, you can instead specify the value of the empty cells by using -empty=(somevalue).  So if your cells consisted of an array of values, such as:     (array:'name','id','phone')

then you'd want the empty value to look like:
 
-empty=(array: ' ',0,' ').

Parameters

-array_of_items array, required An array of elements to format.
-maxcols integer, optional Maximum number of columns.
-maxrows integer, optional Maximum number of rows.
-empty string, optional Value to use for empty cells, default is null.
-fill boolean, optional Fill to -maxcols/-maxrows? Default is to trim cells to dataset size.
-vertical boolean, optional Fill the elements in vertically, default is horizontal.

Sample Usage

[var:'array' = (array:'a','b','c','d','e','f','g','h','i')]

<pre>
<b>-=['['] E X A M P L E S [']']=-</b>

<b>Raw:</b>

[iterate: $array, var:'cell'][$cell]	[/iterate]


<b>Horizontal:</b>
[iterate: (lp_array_columns: $array), var:'row']
[iterate: $row, var:'cell'][$cell]	[/iterate][/iterate]


<b>Vertical:</b>
[iterate: (lp_array_columns: $array, -vert), var:'row']
[iterate: $row, var:'cell'][$cell]	[/iterate][/iterate]


<b>Horizontal -maxcols=2:</b>
[iterate: (lp_array_columns: $array, -maxcols=2), var:'row']
[iterate: $row, var:'cell'][$cell]	[/iterate][/iterate]


<b>Horizontal -maxrows=2:</b>
[iterate: (lp_array_columns: $array, -maxrows=2), var:'row']
[iterate: $row, var:'cell'][$cell]	[/iterate][/iterate]


<b>Horizontal -maxcols=2, -maxrows=2:</b>
[iterate: (lp_array_columns: $array, -maxcols=2, -maxrows=2), var:'row']
[iterate: $row, var:'cell'][$cell]	[/iterate][/iterate]


<b>Vertical -maxcols=2:</b>
[iterate: (lp_array_columns: $array, -vert, -maxcols=2), var:'row']
[iterate: $row, var:'cell'][$cell]	[/iterate][/iterate]


<b>Vertical -maxrows=2:</b>
[iterate: (lp_array_columns: $array, -vert, -maxrows=2), var:'row']
[iterate: $row, var:'cell'][$cell]	[/iterate][/iterate]


<b>Vertical -maxcols=2, -maxrows=2:</b>
[iterate: (lp_array_columns: $array, -vert, -maxcols=2, -maxrows=2), var:'row']
[iterate: $row, var:'cell'][$cell]	[/iterate][/iterate]

</pre>



Returns:

-=[ E X A M P L E S ]=-

Raw:

a	b	c	d	e	f	g	h	i	


Horizontal:

a	b	c	
d	e	f	
g	h	i	


Vertical:

a	d	g	
b	e	h	
c	f	i	


Horizontal -maxcols=2:

a	b	
c	d	
e	f	
g	h	
i		


Horizontal -maxrows=2:

a	b	c	d	e	
f	g	h	i		


Horizontal -maxcols=2, -maxrows=2:

a	b	
c	d	


Vertical -maxcols=2:

a	f	
b	g	
c	h	
d	i	
e		


Vertical -maxrows=2:

a	c	e	g	i	
b	d	f	h		


Vertical -maxcols=2, -maxrows=2:

a	c	
b	d
						

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
[

define_tag:'lp_array_columns',
	-description='Takes an array of items and returns them as an array of arrays, split into even columns.',
	-priority='replace',
	-required='array_of_items', -copy,
	-optional='maxcols', -copy,
	-optional='maxrows', -copy,
	-optional='empty', -copy;


// optional: -maxcols=n, -maxrows=n constrain the maximum # of rows and/or columns
// optional: -vertical OR -horizontal, if the info should be displayed vertically or horizontally
//
// defaults: -horizontal, even columns and rows
//


	// trim rows to just allow data?  or fill to -max?
	local:'fill' = false; // default is to trim
	if: params->(find:'-fill')->size > 0;
		local:'fill' = true;
	/if;

	// find what to put in empty cells
	if: !(local_defined:'empty');
		local:'empty' = null;
	/if;	

	if: #array_of_items->type != 'array' && #array_of_items->type != 'lp_array';
		#array_of_items = (array: #array_of_items);
	/if;

	// datasize
	local:'datasize' = #array_of_items->size;

	if: #datasize == 0;
		return: array;
	/if;

	// get -maxcols and -maxrows
	if: local_defined: 'maxcols';
		#maxcols = integer: #maxcols;
	else;
		local:'maxcols' = integer: 0;
	/if;

	if: local_defined: 'maxrows';
		#maxrows = integer: #maxrows;
	else;
		local:'maxrows' = integer: 0;		
	/if;

	if: !#fill;
		// check for out of bounds
		if: #maxcols > #datasize;
			#maxcols = #datasize;
		/if;
		if: #maxrows > #datasize;
			#maxrows = #datasize;
		/if;
	/if;

	// figure out if max passed
	if: #maxcols < 1 && #maxrows < 1;
		// default
		#maxcols = integer: (math_floor: (math_sqrt: (decimal: #datasize)));
	/if;

	// calc maxcols
	if: #maxcols < 1;
		#maxcols = integer: (math_ceil:(decimal:#datasize / decimal:#maxrows));
	/if;

	// calc maxrows
	if: #maxrows < 1;
		#maxrows = integer: (math_ceil:(decimal:#datasize / decimal:#maxcols));
	/if;	

	if: !#fill;
		// shrink size to datasize, throw away maxrows
		if: decimal:#maxrows - (decimal: #datasize / decimal:#maxcols) >= 1.0;
			#maxrows = integer: (math_ceil:(decimal:#datasize / decimal:#maxcols));
		/if;
	/if;
	
	// temp line
	local: 'line' = array;

	// init the return
	local:'return' = array;
	
	// build empty line
	loop: #maxcols;
		#line->(insert: #empty);
	/loop;	

	// build empty return with proper # of rows
	loop: #maxrows;
		#return->(insert: #line);
	/loop;


	// direction to output data, horizontal or vertical
	local:'direction' = 'horizontal'; // default
	if: params->(find:'-vertical')->size > 0 || params->(find:'-vert')->size > 0;
		local:'direction' = 'vertical';
	/if;	

	// ptrs to where in the return we're at
	local: 'row' = integer: 1;
	local: 'col' = integer: 1;

	// init skip
	local: 'skip' = false;


	// loop through the data and format it
	iterate: #array_of_items, local:'record';
		
		if: !#skip;
		
			// get spot to insert
			(#return->(get: #row)->(get: #col)) = #record;

			if: #direction == 'horizontal';
				#col += 1;
				
				if: #col > #maxcols;
					#col = 1;
					#row += 1;
				/if;
	
				if: #row > #maxrows;
					// no more room
					#skip = true;
				/if;
	
					
			else; // vertical
				#row += 1;
				
				if: #row > #maxrows;
					#row = 1;
					#col += 1;
				/if;
	
				if: #col > #maxcols;
					// no more room
					#skip = true;
				/if;
	
			/if;
	
		/if;
	
	/iterate;
	
	// return the data formatted
	return: #return;

/define_tag;

]

 

Related Tags



Comments

none

Email:


Password:



Newest

Most Popular