Ralph (5) [Avatar] Offline
#1
I have a situation where I have two arrays whose elements mostly overlap.
I need to do some time-intensive processing on values contained in both, so I would like to process one array, checking each element for membership in the other, so that results can be attributed to one or both sources.
Afterwards I would scan the second array and perform the time-intenstive process only on not-previously used values.

Is there a quick way, other than scanning the entire array (of numbers), to find out if an element exists in an array?

Thank you.
tmaher (37) [Avatar] Offline
#2
Re: Finding an element in an array
> I have a situation where I have two arrays whose
> elements mostly overlap.
> I need to do some time-intensive processing on values
> contained in both, so I would like to process one
> array, checking each element for membership in the
> other, so that results can be attributed to one or
> both sources.
> Afterwards I would scan the second array and perform
> the time-intenstive process only on not-previously
> used values.
>
> Is there a quick way, other than scanning the entire
> array (of numbers), to find out if an element exists
> in an array?
>
> Thank you.

Good question! The answer is Yes, of course. The approach I prefer is to form
the values of the array into a string, and then use pattern matchiing to determine
whether a particular value is present there.

For example:

$"='%%'; # choose a "value separator" that won't appear in any value
$value='something';
$" . "@array" . $" =~ /$"$value$"/ and print "$value was found in@array";

Page 44 explains the role played by $".