Re: how to find if the value is exists in the array
If you have a sorted array (I guess there are some array-sort-libraries available at the Mercury download section), there might be faster ways to do it.
You could try to generate a "mirror-array" while adding values to your real array, in which the values of the real array are the index and the indexes of the real array are the values (e.g. comma separated if there are more hits).
Then you could use the "in"-operator on your mirror-array to find the index of the value.
This will take performance when adding values to your array (and the mirror-array), but it will be fast when trying to find a value...