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
Scripting question: finding specific values in arrays
Here is a simple patch that returns the index numbers where the value can be found:
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.
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.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
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
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
In my opinion, yes. But it can be made even simpler: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?
IF (GetValue(hasChanged) = 1) THEN BEGIN
Bjørn S
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
Works like a charm. Thank you sir.
antwan
Who is online
Users browsing this forum: Bing [Bot] and 17 guests
