Welcome to %s forums

BrainModular Users Forum

Login Register

Scripting question: finding specific values in arrays

I need help on a Patch
Post Reply
antwan
Member
Posts: 164
Contact:

Unread post by antwan » 11 Oct 2008, 20:12

Hi,

I'm looking to build a system which would find the indexes of an array that contain a certain value. I reckon this would require some scripting?
So I guess I'd need a script that inputs an array and a float/integer. Then it should go through the array and output an array containing the index numbers that matched to the given float/integer. If none matched, it would return an array of size 0.

I have a little bit of understanding of scripting but would be glad for any help to point me in the right direction. Or is this at all possible to start with?

Thanks a million.

antwan

bsork
Site Admin
Posts: 1334
Location: Asker, Norway
Contact:

Unread post by bsork » 12 Oct 2008, 00:42

Here is a simple patch that returns the index numbers where the value can be found:

Code: Select all

VAR pIn, pOut, pValue : TParameter;
VAR len, i, j : integer;
VAR value : single;

PROCEDURE init;
BEGIN
   pIn := CreateParam('array in', ptArray); SetIsOutput(pIn, FALSE);
   pOut := CreateParam('array out', ptArray); SetIsInput(pOut, FALSE);
   pValue := CreateParam('search value', ptDataFader); SetIsOutput(pValue, FALSE);
   //SetMin(pValue, ?); SetMax(pValue, ?);
END;

// main proc
BEGIN
   len := GetLength(pIn);
   IF (len > 0) THEN BEGIN
      j := 0;
      value := GetValue(pValue);
      FOR i := 0 TO (len - 1) DO BEGIN
         IF (GetDataArrayValue(pIn, i) = value) THEN BEGIN
            SetDataArrayValue(pOut, j, i);
            j := j + 1;
         END;
      END;
      SetLength(pOut, j);
   END;
END.
If you're checking for float values, you might want/need to do some rounding of the check value, inside or outside the script.

You might also want to add some code to avoid the whole script being executed every block even when nothing has changed in the array or with the check value. The two strategies I've used is either (when timing can be a bit sloppy) a counter that kicks off every nth execution block, or an extra HasChanged input. In the latter case I connect all the relevant modules (in this case both the in-array and whatever you connect as the check value) to a HasChanged module, and the ouoput of that to the script.
Bjørn S

antwan
Member
Posts: 164
Contact:

Unread post by antwan » 12 Oct 2008, 21:19

Hello,

Thanks so much. I will proceed to try this out. I'm pretty sure I understand your explanation of the HasChanged strategy. I even think I once made a script that behaved exactly like that (basically the whole execution of the script begins with:
len := GetLength(pIn);
IF (hasChanged = 1) AND (len > 0) THEN BEGIN
Right?

So this is the best way to make the script use least CPU when it's not supposed to do anything?

Once again, thanks for your time.

antwan

bsork
Site Admin
Posts: 1334
Location: Asker, Norway
Contact:

Unread post by bsork » 13 Oct 2008, 10:37

antwan wrote:len := GetLength(pIn);
IF (hasChanged = 1) AND (len > 0) THEN BEGIN
Right?

So this is the best way to make the script use least CPU when it's not supposed to do anything?
In my opinion, yes. But it can be made even simpler:

IF (GetValue(hasChanged) = 1) THEN BEGIN
Bjørn S

antwan
Member
Posts: 164
Contact:

Unread post by antwan » 13 Oct 2008, 10:47

Indeed you're right. And only if that is true does one need to do "len := GetLength(pIn);"
Works like a charm. Thank you sir.

antwan

Post Reply

Who is online

Users browsing this forum: No registered users and 82 guests