I'm looking to see how I can search an array of DINTs to see how many occurrences of a certain value there are in the array. My thought was a FSC but I'm looking to see if there are any other ideas out there.
Here's an idea, but one that is more in the "just because you can, doesn't mean you should" category. It can be done with a FAL instruction in one scan as shown in the attached rung.
There are actually two versions of the FAL, the first handling "normal" numerical values. However, it will not work in an unusual case, one that may occur when dealing with bit patterns such as data from digital I/O modules. This case is handled by the second, more complicated FAL expression.
The general idea is to use the operations available to build a FAL/CPT expression to convert the value zero to one, and any non-zero to zero. This 1/0 result can be accumulated over the examination of each element in the data array. This "examination" is just the arithmetic difference between the value being searched and the array element value (i.e., zero -> found, non-zero -> not found).
The situation is complicated by the fact that DINTs are signed integers.
There may be other ways to create this type of expression. This one works under all the conditions I could think to break it.
(nMatch is the number of times chkValue occurs in the chkData array. All must be DINTs).