LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

For loop index

I have an indented array containing texts that executes one by one. It has nested While and For loops, each terminated with a corresponding End While and End For. When I reach End For I need to find out to which For loop number does it belong (so that I can access its number of iterations from a global). How may I do it? I can't come up with an algotithm that works, although I have tried many possiblities (al without success) for the past few days. Kindly help and take me out of this predicament. Thanks!

Instructions.png

Message Edited by C .DOT on 03-26-2010 05:13 PM
0 Kudos
Message 1 of 23
(3,530 Views)

Writing a parsing algorithm like that will need to be much more defined than the example you have posted. You will need to define constructs for functions (e.g. initialise, power off) and variable declaration (Count). You would need to take into account white space; tabs and spaces between words (power_off)

 

You would quite easily be able to do something with an array of strings and a state machine; Index the array and run the appropriate state.

_____________________________
- Cheers, Ed
0 Kudos
Message 2 of 23
(3,524 Views)
Thats already been handled. Removing white spaces has already been achomplished. All I am stuck with is when the sequence reaches End For I need to know which For it belongs to (first For being 0). Meaning I need a VI to which I pass the index of the currently ececuting End For and the array itself and the VI tells me the number of For that End For belongs to. Then I ca pass that index to a global array (which contains the number of iterations for each For loop) and index out the correct iteration for the correct For. I will / already have managed rest of the details just this Current For Index VI needs be coded. And that's what I need help with.
0 Kudos
Message 3 of 23
(3,503 Views)

Well in any language I can think of, the closing statement / bracket closes the most recently opened structure. What I would do would be to read all of your commands in the array array and convert it to reverse polish notation FILO stack, which would remove a lot of need for the open/close logic you are suggesting.

 

May I ask whether this is just a thought exercise, or if it will be implemented where users will be able to write their own commands

_____________________________
- Cheers, Ed
0 Kudos
Message 4 of 23
(3,499 Views)
Do you have a bigger picture explanation of what your trying to accomplish?  How does this array actual relate to your LabVIEW for loop or while loop in use?
Herrlin

Just trying to spread the LabVIEW love.
0 Kudos
Message 5 of 23
(3,496 Views)
No its certainly not a thought exercise. I have a project that needs to be upgraded. It consistes of sequences of tests which run one by one. Now I need to upgrade it so that the sequences might contain While and For loops (nesting allowed with mixed looping structures). I have solved the While loops problem but For loops are giving me a headache. And I need an algorithm to find the for loop number when the sequence reaches End For (so that the iteration count could be accessed (maintained in a separate global) and decisions could be made whether to continue with next instruction or to push the instructions from the currently executing For upto the End For onto the queue. And I can't think of an algorithm that works.
0 Kudos
Message 6 of 23
(3,491 Views)

First, I would avoid using global variables. There have been many threads which discuss why using globals is not a very good practice.

 

Regarding your specific issues have you considered using a stack to hold you state information. Stacks are widely used in the implementations of compilers and interpreters.  As you encounter a loop statement you would push its information onto the stack. When you encounter an end statement you would pop off its information. This will maintain your order and nesting. Part of the data on the stack could be your termination condition, the currentl loop counter, or other state  information about the loop. When you reach an end statement you pop off the data, evaluate it, update if necessary and either puch it back on the stack to do another iteration or move on to the next statement after the loop.

Message Edited by Mark Yedinak on 03-26-2010 08:33 AM


Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 7 of 23
(3,483 Views)
No this array does not relate to LabVIEW loops. Think of the array as containing test sequence steps and the if the user wants to repeate certain parts of the test sequence he inserts For and / or while tags in the sequence and those test sequence steps that fall between the start and end loop tage gets repeated. For "For" loops user also specifies (separately from the text array) the number of iterations for each for loop. Now when the sequence reaches the End For test tag I need to access those iterations and make decision for either abandoning the loop and proceeding with the next test in the sequence or to push the steps beteen the start For and End For into the queue. All I need is to find the number of For when the sequence reaches an End For. That's where I am stuck and need an algorithm to solve this problem. All the other pieces are in place. But this crucial piece is missing. And your neuron power is required to solve it. I can't seem to think of an algotithm by myself.
0 Kudos
Message 8 of 23
(3,479 Views)

Its a functional global.

 

Lets simplify the problem. I have an array of strings. It contains some random strings and four of these strings "While", "End While", "For", "End For" which maybe placed in the form of nested loops. What I need now is when I reach End For, I pass its index and this array to that VI and that VI tells me which For (a number) that End For belongs to. That's all.

0 Kudos
Message 9 of 23
(3,457 Views)
And that is exactly what I gave you. If you use a stack to track you interpretation of the commands you will be able to track the state information and know which loop you are processing. I am fully aware that these commands do not directly relate to a LabvIEW loop per se. You do however have to interpret them and invoke whatever modules are required to process them. Even if you are not actually going to have a loop structure yourself that you will be invoking (I don't know if you simply pass everything off to some other thing for processing or if your application is fully interpreting and processing the commands) you can use a stack. As you progress through the array you provided when you hit a loop push its data onto the stack. Continue processing statements. If you hit another loop statement push that data onto the stack. When you come to an end statement you pop the data from the stack. You will be guarnateed that this data will correspond to your current loop provided you don't allow statements that simply jump you out of the loop.


Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
Message 10 of 23
(3,456 Views)