[array_unique]

Description

Link: [array_unique]
Author: Jason Huck
Category: Array
Version: 8.5.x
License: Public Domain
Posted: Jun. 18, 2008
Updated: Jan. 01, 0001
More by this author...

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.

Parameters

none


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 + '<br />\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.
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
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

01/28/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.
01/28/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.
Email:


Password:



Newest

Most Popular

Support tagSwap.net