Cancel
Showing results for 
Search instead for 
Did you mean: 

How to skip an unknown identifier in a for loop

Valued Contributor
Valued Contributor

I have a method set up to go through all of the stations in my model looking at specific attributes and using the attributes. 

 

I am currently doing this with a TableFile that contains the path of every object that I care about in the model, contained in the first column of the table file with a data format of object. The method then looks to this tablefile with a for loop and sets each object to a local variable for each run of the loop. That local variable is then used to access the object's attributes. See below:

 

ice_screenshot_20160609-152627.jpeg

 

The problem is that not every object has the specified attributes. Because of that, when the for loop hits an object missing the attributes it creates an error. I tried to avoid this using an If statement and a not equal to, but it hasn't worked. As you can see I tried not equal to unknown, and I originally tried not equal to void. Neither worked. Is there a way to move on if an attribute/identifier is unknown?

 

Thank you!

3 REPLIES

Re: How to skip an unknown identifier in a for loop

Legend
Legend

Hello , 

 

You can test if your station has an attribute called "Efficiency" ! 

 

local index : integer;
       local YourStation : object ; 
local StationHasEfficiency : boolean := FALSE ;
for index := 1 to YourStation .numAttr loop if YourStation .getAttrName(index) = "Efficiency" then -- The station has an attribute called Efficiency
StationHasEfficiency := TRUE ; exitloop 1 ; end ; next;

 

 

Re: How to skip an unknown identifier in a for loop

Siemens Phenom Siemens Phenom
Siemens Phenom

The following code goes through all objects in the Frame and prints those objects which have "Efficiency":

 

is
do
    for local i := 1 to numNodes loop
        local o : object := node(i);
        if not o.isNameUnique("Efficiency") then
            print o;
        end;
    next;
end;

 

Please note that 'isNameUnique' checks for the names of both user-defined and internal attributes/methods. If you only want to check for user-defined attributes, 'getAttrNo' would be the method to use. Unfortunately you first need to check if that object has the method 'getAttrNo', because objects that cannot have user-defined attributes (like global Variables) don't understand 'getAttrNo'. So the check would look like this:

if not o.isNameUnique("getAttrNo") and o.getAttrNo("Efficiency") > 0 then

 

Luckily only the Worker (which is never a node of a Frame) has the internal attribute 'Efficiency', so you can use the simpler (and faster) check.

_________________________________________________________________
Did you like the answer? Then click the Thumbs Up button.
Did the answer solve your problem? Then accept the answer as solution.

Re: How to skip an unknown identifier in a for loop

Valued Contributor
Valued Contributor

Thank you both for your solutions! Took a little bit to get the code working, (I'm not very good with Simtalk 1.0), but I got it adapted for my purposes. 

 

Works perfectly, had no idea I could look at every object in the whole simulation in that way.