The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

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 $".