Counting populated elements in an array

danweston

Member
Join Date
Aug 2017
Location
Machesney Park, IL
Posts
1
Studio 5000 v30.11

Good morning-
Is there an easier way than FSC + CNT to tell how many elements of an array are populated and how many are empty (= 0)?

Looking for a single command if one is available.
 
You could just use a count to keep track of the non-zero values as they are written or cleared from an array. No need to perpetually traverse.
 
Last edited:
The help file says you can use <= >= <> and = etc in a FAL but i couldn't get it to work. If it did you could use that to sum up how many were =0.
 
Just be aware that a zero value is considered to be a valid number.

Also be aware that a configured array is populated with zero values at compile/upload.

Just because it's zero, doesn't mean it's not been written to by the code.

If an element of the array can't be zero, then it is easy enough to detect.
 
Would not a CPT as expression in an FAL work?

No, you cannot put CPT in a FAL expression, just an expression itself.....

Data[FALControl.POS] = 0

... would find the first occurrence of an "empty" data element.

Then you can count this from the .FD bit of the FAL Control tag, reset the .FD and .IN bits, and the instruction will then find the next zero element.

It will take multiple logic scans (the same number of zero elements), to find all the occurrences. Or you could JMP backwards to complete the FAL in one logic scan.

However, I would recommend using a FOR instruction to call a routine that checks each element and counts the "unpopulated" ones.
 
You could just use a count to keep track of the non-zero values as they are written or cleared from an array. No need to perpetually traverse.

That depends on what is writing data into the array...

If it was an external write (e.g. from a HMI, SCADA, or other system, then you cannot know when they are written to.
 
No, you cannot put CPT in a FAL expression, just an expression itself.....

Data[FALControl.POS] = 0

... would find the first occurrence of an "empty" data element.

Then you can count this from the .FD bit of the FAL Control tag, reset the .FD and .IN bits, and the instruction will then find the next zero element.

This is referring to the FSC instruction, not the FAL.
 
Studio 5000 v30.11

Good morning-
Is there an easier way than FSC + CNT to tell how many elements of an array are populated and how many are empty (= 0)?

Looking for a single command if one is available.

No there is not a single command to count zeros in an array.

The easiest solution I can think of is to copy your data into a new array. Then sort the new array and use the FSC to find the first value not equal to zero. This is one place where Allen-Bradley starting with zero comes in handy. The first element not equal to zero is going to be equal to the number of zeros in the array. In this example I entered 5 values into the data array in elements 1,3,5,7 and 9. After sort and File search my FSC Position = 5 Which is the amount of zeros in my data array.

count zero.jpg
 
+1 for COP/SRT/FSC if scan time and memory are not issues. There is a lot more code involved to FSC and count, though it offers potential benefits of spreading over multiple scans and in-place counting.
 
No there is not a single command to count zeros in an array.

The easiest solution I can think of is to copy your data into a new array. Then sort the new array and use the FSC to find the first value not equal to zero. This is one place where Allen-Bradley starting with zero comes in handy. The first element not equal to zero is going to be equal to the number of zeros in the array. In this example I entered 5 values into the data array in elements 1,3,5,7 and 9. After sort and File search my FSC Position = 5 Which is the amount of zeros in my data array.

That's neat... +1 qudos
 
daba, First off thanks. But after retest I found this.

In order to obtain the correct position number if the data array is empty will require the Sort Array Length to be 1 greater than the Data Array Length. This Allows the FSC to find the last element always equal to 0 and report the correct number. The previous example would give the same position number if no values were in the data array or if one value was in the data array. (in my example it would show 9). You still copy and sort the Data Array Length but File Search Length is +1 for the sort array.

count zero.jpg
 
daba, First off thanks. But after retest I found this.

In order to obtain the correct position number if the data array is empty will require the Sort Array Length to be 1 greater than the Data Array Length. This Allows the FSC to find the last element always equal to 0 and report the correct number. The previous example would give the same position number if no values were in the data array or if one value was in the data array. (in my example it would show 9). You still copy and sort the Data Array Length but File Search Length is +1 for the sort array.

Surely you could use .DN AND NOT .FD to put 10 as the answer ....
 

Similar Topics

Hi Guys, Does anyone know a good way to count populated elements in an array? I have 2 arrays of 60 elements which hold strings. When a string...
Replies
29
Views
6,015
Hello! I have an application where I can only have 3 pumps running out of 6. I load the pumpRunning bits into an array. What I need to do is...
Replies
6
Views
3,098
Hello I am looking for tips on how to count the duration of a given function and then how to display it on the hmi panel in the hh:mm:ss format...
Replies
4
Views
1,687
Guys, I know it will be silly but can't get my head around it. I have 3 conveyors, every one on a separate servo drive, and 2...
Replies
25
Views
3,465
The 1734-IB8 has no hardware counting function correct? I am trying to find something to substitute in for a 5069-IB16F (since lead times are...
Replies
3
Views
1,389
Back
Top Bottom