Welcome to %s forums

BrainModular Users Forum

Login Register

'Check for Value in Array' script not working

I need help on a Patch
Post Reply
CREDO
Member
Posts: 39
Contact:

Unread post by CREDO » 24 Nov 2017, 22:50

In this script example from the manual, the value is only found if last one in array. Can anyone explain how to have it found if anywhere in array?
(I am VERY new to attempting anything with scripts, so any insight would be greatly appreciated!)

////////////////////////////////////////////////////
// Check if a value is in the array
/////////////////////////////////////////////////////
var Arr, Found, Valsearch : Tparameter;

Procedure Init;
BEGIN
Arr := CreateParam('ArrayIn',PtArray);
setisOutput(Arr,false);
ValSearch := CreateParam('Value to search',PtDataField);
setisOutput(Valsearch,false); setvalue(valsearch,1);
Found := CreateParam('value found',PtSwitch);
setisInput(Found,false);
END;
//////////////////////////////////////////////////////////////////////
Procedure Callback (n: integer); // Scan the N inputs, and n pick the index of any input that changed to
// return result in same bloc.
var valFound : boolean; // "is our value found?" is a boolean = binary choice TRUE or FALSE type
// of variable
var i : integer; // integer used to scan thru the array elements.
BEGIN // Start the callback = check inputs changes

IF (n=Arr) then begin // if changes on Arr input, start
i := 0; // reset i (=index to scan)
valfound:=FALSE; // by default the value is not found
while ((not valFound) and (i < GetLength(arr))) do begin // do the check for each elmt among array
// length as long as value isn't found
valFound := (GetDataArrayValue(arr, i) = getvalue(Valsearch) ); // boolean result of the check we want to do ,
// TRUE if the array elmt(i) = our input value
i := i + 1; // increment the scanned elmt
end;
if valfound then begin //if the result of valfound is true, we set parameter to 1
setvalue(Found,1);
end
else begin //otherwise we set it to 0;
setvalue(Found,0);
end;
END; //end of subcheck process
END; // END of callback

23fx23
Member
Posts: 2545
Contact:

Unread post by 23fx23 » 25 Nov 2017, 04:37

mm thats strange i did this one a while back, checked here it work as espected, if i put 0.5 in middle of array and looking for 0.5 it will be switched on as found for exemple.
what value are you searching?

CREDO
Member
Posts: 39
Contact:

Unread post by CREDO » 25 Nov 2017, 05:55

I'm searching integers between 1-128. Tried 0.5 too, but same thing, only found if last value... (Fwiw, I'm using 32-bit HH2 w/Windows)

CREDO
Member
Posts: 39
Contact:

Unread post by CREDO » 26 Nov 2017, 05:57

OK, the 64-bit version works so it appears to be a bug in 32-bit HH2/HH3.

User avatar
senso
Site Admin
Posts: 4424
Location: France
Contact:

Unread post by senso » 26 Nov 2017, 12:28

Confirmed on the 32 bit version Only
I'll take a look.
But here is a workaround :

Code: Select all

////////////////////////////////////////////////////
// Check if a value is in the array
/////////////////////////////////////////////////////
var Arr, Found, Valsearch &#58; Tparameter;

Procedure Init;
BEGIN
  Arr        &#58;= CreateParam&#40;'ArrayIn',PtArray&#41;;                 
  setisOutput&#40;Arr,false&#41;;
  ValSearch  &#58;= CreateParam&#40;'Value to search',PtDataField&#41;;
  setisOutput&#40;Valsearch,false&#41;; setvalue&#40;valsearch,1&#41;;
  Found      &#58;= CreateParam&#40;'value found',PtSwitch&#41;;       
  setisInput&#40;Found,false&#41;;
END;
//////////////////////////////////////////////////////////////////////
Procedure Callback &#40;n&#58; integer&#41;;            // Scan the N inputs, and n pick the index of any input that changed to
                                            // return result in same bloc.
var valFound &#58; boolean;                     // "is our value found?" is a boolean = binary choice TRUE or FALSE type
                                            // of variable
var i &#58; integer;                            // integer used to scan thru the array elements.
BEGIN                                       // Start the callback = check inputs changes

IF &#40;n=Arr&#41; then begin                      // if changes on Arr input, start
   i &#58;= 0;                                  // reset i &#40;=index to scan&#41;
   valfound&#58;=FALSE;                         // by default the value is not found
   while &#40;&#40;not valFound&#41; 
   and &#40;i < GetLength&#40;arr&#41;&#41;&#41; 
   do begin           // do the check for each elmt among array
                                                                      // length as long as value isn't found       
      if &#40;GetDataArrayValue&#40;arr, i&#41; = getvalue&#40;Valsearch&#41; &#41;
      then valFound &#58;= TRUE;// boolean result of the check we want to do ,
//      if valfound then itrace&#40;i&#41;;                                                                // TRUE if the array elmt&#40;i&#41; = our input value
      i &#58;= i + 1;                                                     // increment the scanned elmt 
      
   end;
   if valfound then begin                    //if the result of valfound is true, we set parameter to 1
      setvalue&#40;Found,1&#41;;
   end
   else begin                            //otherwise we set it to 0;
      setvalue&#40;Found,0&#41;;     
   end; 
END;  //end of subchec   

end;

Post Reply

Who is online

Users browsing this forum: No registered users and 78 guests