Lasso Soft Inc. > Home

[lp_array]

Linklp_array
AuthorBil Corry
CategoryArray
Version8.x
LicensePublic Domain
Posted22 Nov 2005
Updated30 Oct 2006
More by this author...

Description

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

Sample Usage

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

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

'

Roundrobin Demo

'; $colors->(setindex: 1); loop: $loop; $color = ($colors->roundrobin); '' loop_count ': ' $color '
'; /loop; '

Reverserobin Demo

'; $colors->(setindex: 'last'); loop: $loop; $color = ($colors->reverserobin); '' loop_count ': ' $color '
'; /loop; '

Random Demo

'; loop: $loop; $color = ($colors->random); '' loop_count ': ' $color '
'; /loop; '

Map-Features Demo

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

'; '(notice the order is kept!)

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

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_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

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