Lasso Soft Inc. > Home

[array_unique]

Linkarray_unique
AuthorJason Huck
CategoryArray
Version8.5.x
LicensePublic Domain
Posted18 Jun 2008
Updated18 Jun 2008
More by this author...

Description

This is a subclassed array which will not add duplicate items. Useful in situations where the order of insertion is important, and thus using a [set] is not an option. Otherwise, works identically to a standard array.

Sample Usage

var('test') = array_unique('a','b','c');

$test->insert('a'); // will not be inserted
$test->insert('m'=1); // also works with pairs
$test->insert('m'=2); // same key inserts because value is different
$test->insertfirst('m'=2); // true duplicate does not insert
$test->insert('d');

$test->insertfirst('b'); // will not be inserted
$test->insertfirst('z');

$test->insertlast('c'); // will not be inserted
$test->insertlast('e');

var('more') = array('f','g','h','x','y','z');
$test->insertfrom($more->iterator); // will not (re)insert z

$test; // returns a standard array: z,a,b,c,m=1,m=2,d,e,f,g,h,x,y

// iteration, etc. works as expected.
iterate($test, local('i'));
	#i + '
\n'; /iterate;

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(
	'array_unique', 'array',
	-prototype,
	-description='An array that will only hold unique values.'
);
	define_tag('insert');
		local('v') = @params->first;
		local('p') = (params->size > 1 ? @params->second | self->parent->size + 1);			
		!self->parent->find(#v) ? self->parent->insert(#v, #p);
	/define_tag;
	
	define_tag('insertfirst');
		self->insert(params->first, 1);
	/define_tag;
	
	define_tag('insertfrom');
		local('c') = @params->first;
		
		while(!#c->atend);
			self->insert(#c->value);
			null(#c->forward);
		/while;
	/define_tag;
	
	define_tag('insertlast');
		self->insert(params->first, self->parent->size + 1);
	/define_tag;
	
	define_tag('onconvert');
		return(self->parent);
	/define_tag;
/define_type;

Related Tags

Comments

28 Jan 2011, Bryce Smith

Count elements not added

Great tag. Very useful. Thanks for making it available to us. I am adding a couple hundred unique elements to an array using this tag and was wondering if there was some way to check and see if the element was added each time I loop through. I'd like to make a count of all instances of an element not added, until one is, then start the count over. Is there and undocumented feature for that? Thanks much.

28 Jan 2011, Bryce Smith

Count elements not added

Great tag. Very useful. Thanks for making it available to us. I am adding a couple hundred unique elements to an array using this tag and was wondering if there was some way to check and see if the element was added each time I loop through. I'd like to make a count of all instances of an element not added, until one is, then start the count over. Is there and undocumented feature for that? Thanks much.

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