Searching for values in an array

golftmd13

Member
Join Date
Feb 2018
Location
Missouri
Posts
9
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.
 
I think that the FSC is less efficient, because you would have to search the array for the first occurrence, then un-latch the .IN bit and go another program scan before searching for the second occurrence, and so on.

If you don't mind it taking multiple scans to complete, that's a valid way to do it.

I would probably do it with a simple subroutine, called by a FOR instruction. FOR is similar to JSR, but it calls the subroutine multiple times before continuing on to the next rung.
 
Because I wanted to be sure I wasn't blowing smoke, I did a routine like that.
Of the 100 elements in PLCTalk_Data_Array, five of them are "123", the value of PLCTalk_Match_Value.

Count_Values_in_Array.png
 
Did you do a timing test between FSC and FOR ? I need to check an array of bools for a updates in the number of set (an alarm annunciator) I have working code from a year ago using FSC, but now it looks unwieldy so I want to see if I can improve it.
 
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).

match FAL.jpg
 

Similar Topics

Good morning everyone, I'm working on a project where I am recording data from a machine by sending it message packets. Most of the time the data...
Replies
6
Views
2,144
Hi everybody. I'm working with RSLogix5000 and looking to create a database of part numbers and corresponding critical values. To use this...
Replies
3
Views
2,744
is it just me, or has something happened with the google search when using chrome? looks likes safari on mac is ok, but edge/chrome on windoze has...
Replies
10
Views
830
Besides using BootP in the past, I am relatively new to having to look for privately addressed devices in many control networks. All of the...
Replies
23
Views
3,902
Good morning, could anyone recommend a 24VDC pulse counter which counts up or down to a preset #? Once it reaches this number, send a 24VDC back...
Replies
3
Views
1,645
Back
Top Bottom