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

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

sanaz_karamimoghaddam
Not applicable
366 Views
11 Replies
Message 1 of 12

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

sanaz_karamimoghaddam
Not applicable

[ FlexSim 16.2.1 ]

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?

0 Likes
Accepted solutions (1)
367 Views
11 Replies
Replies (11)
Message 2 of 12

kari_payton
Not applicable

@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

Message 3 of 12

sam_stubbsYXX86
Community Manager
Community Manager

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.

Message 4 of 12

matt_long
Not applicable
Accepted solution

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.

Message 5 of 12

sanaz_karamimoghaddam
Not applicable

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
Message 6 of 12

sam_stubbsYXX86
Community Manager
Community Manager

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

0 Likes
Message 7 of 12

sanaz_karamimoghaddam
Not applicable

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
Message 8 of 12

sanaz_karamimoghaddam
Not applicable

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

0 Likes
Message 9 of 12

sanaz_karamimoghaddam
Not applicable

@Matt Long,

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

0 Likes
Message 10 of 12

matt_long
Not applicable

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.

Message 11 of 12

benjamin_h3
Not applicable

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
Message 12 of 12

braydn_t
Not applicable

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