Link | array_unique |
Author | Jason Huck |
Category | Array |
Version | 8.5.x |
License | Public Domain |
Posted | 18 Jun 2008 |
Updated | 18 Jun 2008 |
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.
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;
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;
©LassoSoft Inc 2015 | Web Development by Treefrog Inc | Privacy | Legal terms and Shipping | Contact LassoSoft
Count elements not added