return multiple indexes of multiple items in a single array


#1

hi,
I have an array with duplicates:
string $myArray [] = {“item”, “something”, “something”, “item”, “other”};
and I want to check indexes for duplicates: item and something.
So I want to get this result: 0, 1, 2, 3

I managed to create an array that says what is duplicated. So I already have this array:
string $duplicates [] = {“item”, “something”};

Now I want to check the indexes of these duplicates in my initial $myArray. I can do this for a single item with a stringArrayFind command using a ’ for loop’ (since stringArrayFind returns only the first appearance not all of them at once):

int $a=0;
for ($x=0; $x < (size $myArray); $x++)
{
int $index = stringArrayFind ( $duplicates[0], $x, $myArray );
$resultIndex [$a++] = $index;
}

but no idea how to do this for multiple items. Of course I could repeat this process for $duplicates[1] and sum the results, but as you may guess this is not what I’m looking for.

Any ideas how to loop my loop?


#2

There’s a pretty basic algorithm for this if I understand your question correctly, but I will say MEL is the uglier option for doing this (compared to python).

for ($dup in $duplicates) {
    for ($x=0; $x < (`size $myArray`); $x++) {
        if ($myArray[$x] == $dup) {
            $resultIndex [$a++] = $x;
    }
}

I haven’t done MEL in a while so I apologize if the syntax is wonky.


#3

From the help:

  string $list[] = { "d", "b", "c", "c", "a", "a", "b" };
  string $shorterList[] = stringArrayRemoveDuplicates($list);