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.

Kari Payton avatar image Kari Payton commented ·

@sanaz karamimoghaddam An off the cuff solution... you could set a label "column value" and read the first row in the table for that column, then have a decide node if the value equals 0 then set a label "row number" to be 1, but if not then repeat the loop but increment the "row number" by 1 until it finds the solution. i realize you asked for code in the custom code so this may not be what you were looking for. row0.fsm

1 Like 1 ·
row0.fsm (16.5 KiB)
sanaz karamimoghaddam avatar image sanaz karamimoghaddam Kari Payton commented ·

Thanks for your response @Kari Payton, That would work too, but my model is too crowded already so I am trying to do things with minimum labels of such nature.

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

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

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

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

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.

sanaz karamimoghaddam avatar image sanaz karamimoghaddam commented ·

@Sam Stubbs and @Matt Long Thank you both for your answer!

0 Likes 0 ·
sanaz karamimoghaddam avatar image sanaz karamimoghaddam commented ·

@Matt Long,

is "count", a predefined variable since it appears in blue, or should it be another name?

0 Likes 0 ·
Matt Long avatar image Matt Long sanaz karamimoghaddam commented ·

You can use count. It's blue because there are some special commands that pass values into count like findmatch(). But it works just like item and current or i, you can use it wherever.

1 Like 1 ·
Benjamin H3 avatar image Benjamin H3 commented ·

Hello, I need some help with the coding on flexsim I am an absolute beginner and was wondering if anyone knew the coding language for the statement below

//Go to Global table

//Look at Row 1

// Check Column 1 value

// If value >0, then value = value -1

// Result = column number (holding area 1 output port 1 connected to staging area 1

// Break loop

// Else

// Check column 2 value

// Loop


0 Likes 0 ·
Braydn T avatar image Braydn T Benjamin H3 commented ·

Hi @Benjamin H3

Thanks for using FlexSim. Please follow our community Best Practices that are posted in the top right corner and post this in a new question. Thank you!

0 Likes 0 ·
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:

int returnRow = 0;
int returnCol = 0;
for (int col=1; col<gettablecols("GlobalTable1"); col++)   {
	for (int row=1; row<gettablerows("GlobalTable1"); row++)  {
		if (gettablenum("GlobalTable1",row,col)==0)  {
			returnRow = row;
			returnCol = col;
			break;
		}
	}
}

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.

sanaz karamimoghaddam avatar image sanaz karamimoghaddam commented ·

Thanks for your response @Sam Stubb

in this code, if I already know the column that it needs to check for, should I say:

for (int row=1; row<gettablerows("GlobalTable1"); row++)  {
for (int col=1;) {
                   
0 Likes 0 ·
Sam Stubbs avatar image Sam Stubbs ♦ sanaz karamimoghaddam commented ·

Yes just eliminate the outer for loop if you've already got the column.

0 Likes 0 ·