Angie Ahl wrote:
> Hi List
Hello,
> I've got a way to do this but I thought someone more experienced than
> me might have a better way.
>
> I have a class that is a hash and one of the hash values is an array ref.
>
> 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);}
That won't work because grep() aliases $_ so grep { /^$_$/ } is actually
grep { $_ =~ /^$_$/ } which just matches every element of @used_images
with itself. It also won't work if any element of @used_images contains
regex metacharacters.
You want something like:
for my $img ( @imgids ) {
push @used_images, $img unless grep $img eq $_, @used_images;
}
> $_[0]->{_used_images} = [EMAIL PROTECTED];
>
> Anyone know of a way of doing it without dereferencing it.
for my $img ( @imgids ) {
push @used_images, $img unless grep $img eq $_, @{ $_[ 0 ]->{
_used_images } };
}
John
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>