Mr. Shawn H. Corey wrote:
: OK, here's a solution that might be faster. The problem with
: it is as_array() which has to scan the list every time. There
: is not simpler way for it to work.
We could do a unique check only when the array is accessed
instead of every time a value is added. Then we used the cached
result until another element is added.
use strict;
use warnings;
use Tie::Array::Unique;
print join( ' ', as_array() ), "\n";
for my $element ( qw( a b c b a ) ) {
add_element( $element );
print join( ' ', as_array() ), "\n";
}
for my $element ( 'a' .. 'z' ) {
add_element( $element );
}
print join( ' ', as_array() ), "\n" for 1 .. 10;
BEGIN {
tie my @unique, 'Tie::Array::Unique';
my @added;
my $max = 20;
my @max_range = 0 .. $max - 1;
sub add_element {
unshift @added, shift;
}
sub as_array {
# return cache unless something added
return @unique unless @added;
# add new elements
unshift @unique, @added;
@added = ();
return @unique unless @unique > $max;
# set max
@unique = @[EMAIL PROTECTED];
return @unique;
}
}
__END__
HTH,
Charles K. Clarkson
--
Mobile Homes Specialist
Free Market Advocate
Web Programmer
254 968-8328
Don't tread on my bandwidth. Trim your posts.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>