question

Serge A avatar image
2 Likes"
Serge A asked Serge A commented

Get a Table row / check if it exists without exceptions

Currently (FlexSim 17.0 & 17.1), Table class has a index operator which allows to get rows by name, but it fails with an uncatchable exception if a row with such header does not exist.

This syntax is nice:

Table tbl = somenode;
return tbl["somerow"][column_name_or_number];  // OK

But there is a problem. Table class doesn't provide any method to check if there is a row with a given name in the table, and there is no way to handle exceptions in FlexScript.

This code throws an exception:

Table tbl = somenode;
return tbl["nonexistent_row"][column_name_or_number];

The only workaround to avoid exceptions is to explicitly iterate over all Table rows, and do not use the index access at all. In the long term, an exception handling mechanism in FlexSim would help.

Could you please return a nullvar if the row (or column) doesn't exist? And/or add a way to check if there is a row or column with a given name?

FlexSim 17.0.6
flexscripttables
5 |100000

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

Phil BoBo avatar image
4 Likes"
Phil BoBo answered Serge A commented

I will add a note to the dev list to improve this. You are correct that it should either return a nullvar or we should add a way to check if the cell exists. We will probably update it so that it returns a nullvar to match how getting labels and subnodes by name works.

Thanks Serge.

· 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.

Serge A avatar image Serge A commented ·

Thank you, Phil. Returning nullvar sounds like a fine solution.

0 Likes 0 ·
Jacob Gillespie avatar image
4 Likes"
Jacob Gillespie answered Serge A commented

I think you are right that the class doesn't give you any way to do that.

However you could do something like this:

Table table = Table("GlobalTable1");
string row = "Row 1";
string col = "Col 1";
if(table.as(treenode).find(row + "/" + col))
	return table[row][col];
· 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.

Serge A avatar image Serge A commented ·

Thank you, right now I just write loops like

for (int i = 1; i <= tbl.numRows; i++) {
	if (tbl.getRowHeaders() == "whatever") {
		// ...
	}
}

but the new syntax is certainly nicer.

0 Likes 0 ·