Sorting String data using Structured Text

kpElec

Lifetime Supporting Member
Join Date
Oct 2016
Location
Minnesota
Posts
23
Hi all,

I have a customer that is asking for a recipe of 200 recipes to be sorted. I'm trying to do this by way of a bubble sort.

Basically my code looks like this but pay no attention to the syntax. I will sort that out, no pun intended.

For a = 0 to 199 -1
If recipe[a]+1 <> "" then
For nSort = 0 to 199 -1
If recipe[nSort] > recipe[nSort]+1 then
Copy ( recipe[nSort], recipeTemp)
Copy ( recipe[nSort]+1, recicpe[nSort])
Copy (recipeTemp, recipe[nSort])
End_If;
End_If;
End_For;
End_For;

Much of the recipe array has empty data, (i.e. "" ) for the recipe name so I need to handle the null data as well.

I looked in the data base but can't find much information for ST programming on Sorting.

I would appreciate any ST examples that anyone has.
Thank you.
 
https://en.wikipedia.org/wiki/Bubble_sort




Also, your recipe[index]+1 should probably be recipe[index+1]


Also, instead of the outer repeat in that wiki, you could have two loops:


Code:
Code:
[COLOR=Blue][COLOR=Black]for aouter = 0 to 199[/COLOR][/COLOR]
  for ainner from 0 to (199-aouter)
    ...
so the first outer pass moves ("bubbles") the largest recipe ("up") to slot 199, the second bubbles the second largest up to slot 198, etc.




Update: the final bit of pseudocode on the wiki does what is suggested here and a bit better.
 
Last edited:
Storage of Strings

I think the initial answer depends on finding out if the processor and compiler supports a string data type natively or if the name strings are packed into another data type like an array of integers. That makes a big difference as to how the comparison is coded.
 
Thank you all for responding.

Yes, I agree the the correct method should be [index+1] so thank you.
The recipes start at array 1 and then proceed toward element 199 so I believe I would for ainner from 0 to (199+aouter) as I would need to examine between the outer element loop to the inner loop. If the outer loop element is greater than the inner element, I would want to swap the two examined words, correct?

BTW, this program is being done on a 1756-L72 control logix.

As a note to Corsair, I'm very confident the sorting of strings can be done.

Thanks for the link to the bubble sort.
If anyone has a ST example they could share, that would be great!

Thank you again.
 
PLC-Resident Recipes

I agree that PLC storage of recipes has problems but with smaller systems with a limited recipe count it can work quite well. I've done it several times. You don't have to worry about communications hiccups on download, all the HMIs can see the recipes, and the HMI-PLC download handshake is not an issue. I'm doing a batching system right now with PLC-resident recipes that are backed up on a memory card. It's a CPU changeout of a system that I did 20 years ago and the users prefer to not have PC-resident recipes.

One downside is that HMI tag counts and development work can get to be really excessive if the HMI does not natively support Arrays of PLC data. For the system that I am doing it makes the difference between about 5 tags and over 6000 tags.

As with all architecture choices, it depends on the specific application and the software that is used.
 
And ..

I forgot to mention that I'm doing a bubble sort in it. Don't have any code that I could send to you at this point.
 
No doubt, storing recipes in a PLC is just wrong.

I disagree. The PLC has to be usable when the PC is not available.

Storing recipes in the PLC is a must for most applications.

And it does not depend on the complexity of the recipe, nor does it depend on the connection to the PC. Operator selects a recipe, he can make it.

A PLC should not, unless absolutely necessary, be reliant upon another device that may not be online.

The only time a PC need be involved is if the number of recipes is too large for the PLC to handle, or if the management of the recipes involves too many if/buts.

Code recipe handling into the PLC if possible, and walk away knowing that no MS upgrade is going to mess it up, you have control of the situation.
 
It always does in my applications.


I'm quite interested if you would like to expound.

Following your comments, does that mean that with the recipes in the PLC you do the sorting in a PC and somehow insure that the sorting is not required to run a process?
 
I'm quite interested if you would like to expound.

Following your comments, does that mean that with the recipes in the PLC you do the sorting in a PC and somehow insure that the sorting is not required to run a process?

I'm just inquisitive why you need to to "SORT" the recipes, can't you just sort the indexes that access the recipe data ?
 
I'm quite interested if you would like to expound.

Following your comments, does that mean that with the recipes in the PLC you do the sorting in a PC and somehow insure that the sorting is not required to run a process?

No, my recipes are stored outside of the PLC, I send the active recipe data to the PLC. This data doesn't change when/if the PC/HMI is disconnected. Even if you store the recipes in the PLC, you still have to have a way to select them.

In my manner the recipes can be sorted in many ways, names, date created, data last used, most used, etc. Try doing that with the PLC. :)
 

Similar Topics

I have worked on small projects using AB Micrologix but now we want to take a photo, process it online, and sort based on returned variables...
Replies
5
Views
315
Hi, I have just started with WinCC unified (v17) and there are alot of things I used on Comfort but now are not available. Currently I am finding...
Replies
3
Views
2,813
I'm trying to be smart about naming my tags so things automatically group together alphabetically, but for some reason it doesn't work like I...
Replies
15
Views
3,633
Hello friends. There is an int type, number that I got from the DataBlock. For example, the DataBlock address is Db1.dbw0. Here's what I want to...
Replies
32
Views
10,434
Good afternoon all, I am working on a college project where we have to sort 3 different color legos. Orange, Black, and Tan. I am using a color...
Replies
30
Views
6,763
Back
Top Bottom