question

sanaz karamimoghaddam avatar image
0 Likes"
sanaz karamimoghaddam asked Braydn T commented

How to find the first row that has a particular value in a specific column in a global table?

Hi, I want a token to read the values in a column of a global table, and return the first row that has the value 0.0 in that column. I would appreciate any suggestions regarding if there is a command or code that I can use on custom code in process flow?

FlexSim 16.2.1
global tablecustom codelook up
· 2
5 |100000

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

Matt Long avatar image
2 Likes"
Matt Long answered Braydn T commented

You can use the query command. It would look something like:

  1. query("FROM GlobalTable1 WHERE ColumnName == 0");
  2. int matchedRow = getquerymatchtablerow("GlobalTable1", 1);

If you want to know how many rows matched you would use:

  1. int count = getquerymatchcount();

If count > 1 then you can use the second parameter in getquerymatchtablerow() to give you the row values of all of the matches. If count can ever equal 0, you'll need to check the count before calling getquerymatchtablerow() as passing in an invalid index will result in an exception being thrown.

· 5
5 |100000

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

Sam Stubbs avatar image
3 Likes"
Sam Stubbs answered sanaz karamimoghaddam edited

I would use a "for loop" in your logic to search the table for the first value that reads 0. You could use something like this code to achieve that:

  1. int returnRow = 0;
  2. int returnCol = 0;
  3. for (int col=1; col<gettablecols("GlobalTable1"); col++) {
  4. for (int row=1; row<gettablerows("GlobalTable1"); row++) {
  5. if (gettablenum("GlobalTable1",row,col)==0) {
  6. returnRow = row;
  7. returnCol = col;
  8. break;
  9. }
  10. }
  11. }

This will parse through each row and column of the table until it finds a cell that equals 0. Then it will assign the row and column to the returnRow and returnCol variables, which you can use in your logic.

· 2
5 |100000

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