> I'd like to search that array and push to it if a value isn't there. > At the moment I'm dereferencing the array, searching/pushing it and > the passing it back to the class as an array ref again. So this mean > copying the array. Code: > > my @used_images = @{$_[0]->{_used_images}}; > foreach (@imgids) {push(@used_images, $_) unless (grep { /^$_$/ } > @used_images);} > $_[0]->{_used_images} = [EMAIL PROTECTED]; >
The way you wrote it is very confusing, and I actually doubt you get the result you want (too lazy to run it). Avoid relying on $_ when you have to use it with different values on the same line (e.g. you push $_ to @used_images AFTER it has been reassigned by the grep on the last element of @used_images). To answer your actual question - no you don't have to copy the array. You can work on an aray behind a reference just like you did the assignment to @used_images in the first line. Also using eq instead of a regex (which in your case is equivalent to an eq anyway) is times faster. foreach my $new_id (@imgids) { unless (grep { $_ eq $new_id} ( @{$_[0]->{_used_images}} ) ) { push @{$_[0]->{_used_images}}, $new_id; } } And last but not least - if you need unique values why don't you just use a hash for this data branch? If @imgids contains 500 elements and $_[0]->{_used_images} contains 5000 you will run through the inner loop exactly 2500000 times which will cost you even on a very fast machine. Peter -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>