06-09-2020 03:07 PM
I am looking for a clean way to turn an array of integers into a ranges list.
turning this into this
or at least this
I am hoping there is a VI already somewhere in the bowels of Labview that I can use as this is essentially what is happening in the case structure selector label if you list many integers and some are consecutive.
Anyone know where I might find this magic vi? I can write my own but if its already available all the better.
Solved! Go to Solution.
06-09-2020 03:17 PM
I doubt it, you'll likely have to code it yourself.
Should be a fairly simple loop with just a couple case structures.
You need to know when you are starting a run of numbers, ending a run of numbers, or still in the middle.
Step through element by element. If the next element is equal to the current element + 1, you know you are in a run.
06-09-2020 05:20 PM
I know the case structure will automatically do that for you. I wonder if there's a way to leverage that information somehow...
06-09-2020 06:31 PM
I made this ages ago.
06-10-2020 04:28 AM - edited 06-10-2020 04:44 AM
@tshurtz wrote:
Anyone know where I might find this magic vi? I can write my own but if its already available all the better.
I am sure this has been solved before here in the forum. But should be trivial to implements if we can make certain assumptions:
Assuming the input is clean, this might work for you. There are many other ways.
06-10-2020 10:09 AM
Thanks,
I made a few minor modifications to handle a few more fringe cases.
- I made a case if the array is empty to return a blank string
- I sorted the array so that out of order isn't an issue
- I added "0" to the -1 case (compared to difference between current and last value) to ignore repeat values
Thanks again
06-10-2020
10:40 AM
- last edited on
05-05-2025
03:56 PM
by
Content Cleaner
As I said, it really depends how clean the input array is and additional code might be needed.
One request thought. Please give a VI a new name when attaching a new version. It can lead to serious confusions if a thread contains multiple different VIs, all with the same name.
At this point I would suggest to turn it into a malleable VI (vim), correctly accepting any integer datatype. (only available in recent versions, but alone worth the upgrade!)