Counting Populated Elements In An Array

Join Date
Oct 2019
Location
UK - portsmouth
Posts
28
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 holds no data '' its population should be 0
when a string holds data 'ABCD' its population should be 1
I need the total population of both arrays.

Working in Logix 21.11

Seen a similar post for integers but with no clear answer.
http://www.plctalk.net/qanda/showthread.php?t=112140
 
https://www.plctalk.net/qanda/showthread.php?t=116943

summat like
Code:
(stringarray[ctl.pos].len > 1) + ARRAY_TOTAL
I.e. assuming the logical expression (stringarray[ctl.pos] > 0) is cast to an INT value of 0 or 1?

or this:
Code:
(stringarray[ctl.pos].len/(stringarray[ctl.pos].len + 1e-38)) + ARRAY_TOTAL
the {+ 1e-38} ensure it does not divide by 0 when .LEN is 0, without affecting the desired INT result of 1 when .LEN > 0.
 
Last edited:
So I've set it up how the other post said, I'm struggling with the expression which I think is what you have sent me in the last post.

(stringarray[ctl.pos].len > 1) + ARRAY_TOTAL

what do you mean by ctl.pos, would that be my FAL control FAL_L1.POS??

I've attached what I've got so far

FAL Unverified.png
 
Probably don't want .len in those expressions.

I'm pretty sure he does. An unpopulated array element should have a length of 0, a populated element will have a length of at least 1.

EDIT: However, the error is caused by the fact that '>' (and the rest of the comparison operators) are not valid operators for the FAL instruction. See the table beginning on page 341

In other words, the assumption made by drbitboy in post #2 is not valid when using FAL.

The linked topic works not because it is an INT rather than a STRING, but because it is summing the actual contents of the array without any attempt to evaluate them beforehand.
 
Last edited:
I'm pretty sure he does. An unpopulated array element should have a length of 0, a populated element will have a length of at least 1.

EDIT: However, the error is caused by the fact that '>' (and the rest of the comparison operators) are not valid operators for the FAL instruction. See the table beginning on page 341

In other words, the assumption made by drbitboy in post #2 is not valid when using FAL.

The linked topic works not because it is an INT rather than a STRING, but because it is summing the actual contents of the array without any attempt to evaluate them beforehand.
That makes sense, So FAL functions cannot use comparison operators.
Does that rule out using the FAL to search the population of an array?

Any other ideas on how I can search the array population?
 
Any chance the array elements will always have the same length? Or can be buffered so that they do? If so, you could sum the total number of characters and then just divide by the known length when done. A but of a kluge, but should work.
 
Basically just 'EM005_STATION5_Robot1OutPuckRFID_FIFO[FAL_L1.POS].LEN + FAL_L1_Total'

Then DIVide FAL_L1_Total by 16 as an operation separate from the FAL.

EDIT: As destination unkown points out, FSC could work instead of FAL if there is any part of the string that will always be the same.
 
Last edited:
RA likely has more elegant ways to do it, but a For loop incrementing an int if indexed position not equal to empty string is a simple way that works on others as well.
 
or, to do it in one step, assuming .LEN can only be either 0 or 16, though less efficiently:
(EM005_STATION5_Robot1OutPuckRFID_FIFO[FAL_L1.POS].LEN/16) + FAL_L1_Total
Alternatively, if this is indeed a FIFO, then add/subtract 1 to a running total when a non-empty string is FFLed-onto/FFUed-from the FIFO.
 
Last edited:

Similar Topics

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...
Replies
14
Views
7,528
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,133
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,700
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,499
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,414
Back
Top Bottom