question

Richard G avatar image
0 Likes"
Richard G asked Richard G commented

Nested If Statement Within for

Hello - I developed custom code that writes to a set of tracked variables on a queue as labels. If the TVs and labels don't exist, then an if statement generates and initializes them. I tested the if statement and confirm it works as designed for a single queue. However, my model has many queues that must be tracked, so I've chosen to nest the if statement and iterate through a list (global table) of my TV names, so all labels will be generated automatically.

My issue: the if statement is not run. I set a breakpoint and checked line by line, and the first lines within the for loop work correctly, but the first line of the if returns the compiler to the first line within the for, and once the end of the counter is reached the loop is exited and the rest of the code continues. If anyone can put me on the right path I'd appreciate it! Code:

string qName = getname(current);
Object Queue = model().find(qName);

for (int i = 1; i <= totalTVs; i++){      

string tableVal = gettablestr("TVs",i,1); // get TV name from global table "TVs"

if (model().find("/" + qName + ">labels/" + tableVal) == NULL) // execute if labels not found   

{     

treenode tv = labels(Queue).subnodes.assert(tableVal);   // generate label, assert name     

TrackedVariable.init(tv, STAT_TYPE_LEVEL, 0, STAT_USE_HISTORY | STAT_USE_PROFILE | STAT_IGNORE_WARMUP); // generate TV as label    

 settrackedvariable(tv, 0);   // set initial value  

 }

}
FlexSim 18.0.1
tracked variablesfor loopif loopnested
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

Matthew Gillespie avatar image
2 Likes"
Matthew Gillespie answered Richard G commented

That looks like it should work. I took your code sample, cleaned it up a bit, and then put it in a user command in this sample model. It's working just fine. I call the user command in the On Reset trigger of each queue.


asserttvlabels.fsm (17.8 KiB)
· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Richard G avatar image Richard G commented ·

Excellent, thank you! I can also confirm the original code works - I was debugging the wrong queue lol - but it's great that you took the time to clean up the sample, and I'll use yours going forward.

0 Likes 0 ·