Lasso Soft Inc. > Home

[lp_array_columns]

Linklp_array_columns
AuthorBil Corry
CategoryArray
Version8.x
LicensePublic Domain
Posted22 Nov 2005
Updated05 Jun 2006
More by this author...

Description

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,' ').

Sample Usage

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

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

Raw:

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


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


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


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


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


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


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


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


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

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.

[

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

No comments

Please log in to comment

Subscribe to the LassoTalk mail list

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft