question

Chris Ligetti avatar image
0 Likes"
Chris Ligetti asked Jacob Gillespie commented

How to set label based on string

I have a string assigned that needs to map to a label name. For simplicity:

string label = "numShifts";

I then want to set the label called "numShifts" on an object ("Processor1" in my example). I tried using the below command, but am getting a compiler error. Anyone know how I could do this.

model().find("Processor1").labels[label] = 2;

flexscript
5 |100000

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

1 Answer

·
Jacob Gillespie avatar image
2 Likes"
Jacob Gillespie answered Jacob Gillespie commented

You need to use assert() to make sure the label exists. Also the label array gives you access to the label node so you have to use value.

string label = "numShifts";
model().find("Processor1").labels.assert(label).value = 2;
· 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.

Chris Ligetti avatar image Chris Ligetti commented ·

@Jacob Gillespie, that appeared to work well. Thank you so much. Should I be concerned with the following exception I seem to get when I run that script?

exception: FlexScript exception: /0 c: /testlink_instance i: /testlink_associated

Here is the full script that is loading, where I am opening a csv file, using the string tokenizer to set some string variables, and using those variables to set the labels on model objects.

fileopen("C:/Users/cxl300/Desktop/StationAttributes.csv","r"); // open the csv file


filereadline(); // read and disregard the header line


while(endoffile!=1){
	string line = filereadline(); // read the line
	
	string station = stringtoken(line,","); // grab the station
	string attribute = stringtoken(NULL,","); // grab the attribute name
	string value = stringtoken(NULL,","); // grab the attribute value
		
	model().find(station).labels.assert(attribute).value = value.toNum();
		
}


fileclose; // close the file

0 Likes 0 ·
Jacob Gillespie avatar image Jacob Gillespie ♦ Chris Ligetti commented ·

@Chris Ligetti It means that find() is not finding anything.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

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