ST search an array for a value

MikeLind

Member
Join Date
Apr 2013
Location
Frederikshavn
Posts
7
Hi I new to ST programming and I am trying to figure out how I can search an array for an value (3).

I want TEST to go high if eg trayArray[6] = 3 and low if none of the trayArray[j] is 3.


VAR
TEST: BOOL;
trayArray: ARRAY [0..15] OF INT;
j: INT;
END_VAR



FOR j:= 0 TO 15 DO;

IF trayArray[j] = 3 THEN
TEST := TRUE;
ELSIF
trayArray[j] <> 3 THEN
TEST := FALSE;
END_IF

END_FOR



Is what i got but it aint working :s any suggestions? tried google already :(
 
Your code should work.
Your code wont work because even if a match is found an test is set true, at the next array element that does not match the same variable is set back to false.
This will work:
Code:
temp_test:=FALSE;
FOR j:= 0 TO 15 DO;
  IF trayArray[j] = 3 THEN 
    temp_test := TRUE;
  END_IF ;
END_FOR ;
test := temp_test ;

Is you array initilized ?
You can try to add this line
trayArray[1] := 3;
 
Last edited:
Your code should work.
Your code wont work because even if a match is found an test is set true, at the next array element that does not match the same variable is set back to false.
This will work:
Code:
temp_test:=FALSE;
FOR j:= 0 TO 15 DO;
  IF trayArray[j] = 3 THEN 
    temp_test := TRUE;
  END_IF ;
END_FOR ;
test := temp_test ;

Is you array initilized ?
You can try to add this line
trayArray[1] := 3;

Jesper you are the man! :)

Is this the optimal way to go about the search for number 3 or is there a more "simple" way to get the same result. I always try to keep the code as short as possible :)
 
You could remove the temp_test and use test directly.
I use the temporary variable so that test variable doesnt "flicker". For example, if the test variable were displayed on an HMI, the flickering would confuse the operator.
 
I don't know ST very well but something like this may be simpler:
Code:
j := 0;
while j < 16 do;
  if array[j] = 3 then
    break;
  end_if;

  j := j + 1;

end_while;
test := j < 16;



Does ST have [while...do] and [break]? If it has BREAK, then maybe it can break out a FOR loop:



Code:
for j := 0 to 15 do;
  if array[j] = 3 then
    break;
  end_if;

end_for;
test := j < 16;


Also, it would be nice if the IF did not require THEN and END_IF:



Code:
  if array[j] = 3 break;
 
Does ST have [while...do] and [break]? If it has BREAK, then maybe it can break out a FOR loop:
Code:
for j := 0 to 15 do;
  if array[j] = 3 then
    break;
  end_if;

end_for;
test := j < 16;

Also, it would be nice if the IF did not require THEN and END_IF:

Code:
  if array[j] = 3 break;

Not break but there is an EXIT command that is used in the same way.
You do need the THEN/END_IF
 
Last edited:

Similar Topics

when you have an array of 100 DINT's... And you enter in a wincc flex I/O field a DINT value to search for. How can you do that and know the...
Replies
3
Views
2,351
Hi I am stuck in trying to get my code search an array and retur an index number. So, I have an array from 0 to 254 created from an UDT where I...
Replies
7
Views
2,467
Hi guys, I have a small project that I am working on, I have this station that a unit enters, an RFID reads the unit's serial ID then it starts...
Replies
16
Views
3,383
A customer is asking if I can do a check on his batch numbers and reject a batch from starting if the check fails. The setup is as follows: I have...
Replies
5
Views
1,577
Searching edit zones within all routines and other criteria still says no results found, yet I still have the "edits present" indicator at the...
Replies
6
Views
255
Back
Top Bottom