Rockwell Automation Publication 1756-RM003N-EN-P - October 2011 689
Structured Text Programming Appendix C
Example 2:
If you want this Enter this structured text
A user-defined data type (structure) stores the following information
about an item in your inventory:
·Barcode ID of the item (string data type)
·Quantity in stock of the item (DINT data type)
An array of the above structure contains an element for each
different item in your inventory. You want to search the array for a
specific product (use its bar code) and determine the quantity that is
in stock.
1. Get the size (number of items) of the Inventory array and store the
result in Inventory_Items (DINT tag).
2. Initialize the position tag to 0.
3. If Barcode matches the ID of an item in the array, then:
a. Set the Quantity tag = Inventory[position].Qty. This produces the
quantity in stock of the item.
b. Stop.
Barcode is a string tag that stores the bar code of the item for
which you are searching. For example, when
position = 5, compare Barcode to Inventory[5].ID.
4. Add 1 to position.
5. If position is ≤ to (Inventory_Items -1), repeat 3 and 4. Since
element numbers start at 0, the last element is 1 less than the
number of elements in the array.
Otherwise, stop.
SIZE(Inventory,0,Inventory_Items);
For position:=0 to Inventory_Items - 1 do
If Barcode = Inventory[position].ID then
Quantity := Inventory[position].Qty;
Exit;
End_if;
End_for;