08-10-2015 09:06 AM
HI
I am trying to stop a while loop after the last element of an array, but the problem is that the last element of the array is subject to change even when the program is running. Any ideas?
08-10-2015 09:09 AM - edited 08-10-2015 09:09 AM
08-10-2015 09:09 AM
You can have any combination of logical comparisons to figure out if it's the last element in the array...
But why don't you just use a For loop? If a For loop isn't what you're looking for then there's probably a better way to do what you're trying to do. Sharing your code is the best way for us to help.
Cheers
--------, Unofficial Forum Rules and Guidelines ,--------
'--- >The shortest distance between two nodes is a straight wire> ---'
08-10-2015 09:12 AM
Hi fg,
this seems related to your old thread, so please don't create double posts and stick with your other thread!
08-10-2015 09:14 AM
What exactly does that mean?
Does that mean the value of the last element will change? Or which element is last may change (the array gets longer or shorter as you process it)?
If the array is changing while it is running, what is causing it to change? Code inside the loop, or other code running in parallel external to the loop?
08-10-2015 09:58 AM
Sorry about creating a double post on this issue. I cannot use a for loop because the code that is suppossed to stop the while loop, along with the array and everything is inside the while looP. If I use a For loop, I do not know how I would index it. Also to respond to what someone said, the array might get longer or shorter if the user who is running the measurement decides to change the number of elements in the array, so the change in length of the array is managed by a user.
Thanks
08-10-2015 10:12 AM
Is there a chance that the user could change the size/contents of the array while you are trying to read in the loop, or you just don't know ahead of time.
If it's just that you don't know ahead of time, the Auto-indexing terminal of the for-loop will take care of you. Whatever the array size is, the for-loop will run once for every loop and then stop at the end.
But if the array can change size/contents while you are trying to read it, that sounds like a mess. Will they always be adding elements to the end, or could they be adding elements anywhere? If it's anything but the end, you could miss stuff and it sounds like the whole approach sounds like it needs to be reworked.
But, I guess you could try compare the i terminal to the size of the array. If your current element is ==size-1, then you are at the end of the array, so stop the loop. This is assuming you are using the i terminal of the loop to index your array.
I think you should try to consider some of the suggestions in the other thread. Those guys are really good at LabVIEW, and since you posted your code in there they were able to give you pretty specialized advice. It might sound confusing, but it's probably the advice you need.
08-10-2015 10:18 AM
Just use DataFlow. Here I'm illustrating this using a Sequence structure -- I (almost) never use these, but would strongly recommend instead having two sub-VIs, one called "Get Next Array Element" and the second called "Check for Last Element" that contains the logic to decide if you are finished (it should have as inputs any and all information it needs to decide if it is time to stop). In this trivial example, the "stop" decision is set to "True". The important point is that you (probably) need to delay the stop decision until after you take what may become the last Array Element -- DataFlow allows you to do that.
Bob Schor