question

Ankur A3 avatar image
0 Likes"
Ankur A3 asked Jason Lightfoot commented

Fetching Global Variable Value Using String Variable?


Hi Team,

I am using string variable to get global variable value. But it is writing variable name instead of value. What is the right way to do it?


1654008091820.png

GlobalVariableUsingStringVariable.fsm

Thank you!

FlexSim 21.0.10
flexscriptstringglobal variable
5 |100000

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

Matthew Gillespie avatar image
0 Likes"
Matthew Gillespie answered Jason Lightfoot commented

You can use the executestring() command to execute a text string as FlexScript code. For example:

int val = executestring("Table" + "Value");
· 22
5 |100000

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

Ankur A3 avatar image Ankur A3 commented ·
Thank you so much! @Matthew Gillespie
0 Likes 0 ·
Ankur A3 avatar image Ankur A3 commented ·

Hi @Matthew Gillespie,

This solution is working fine if I want to write global variable value.

I also want to change global variable value in similar fashion. It is not working in the same way. What is the right way to do it?

1654061744882.png

Thank you!

0 Likes 0 ·
1654061744882.png (76.4 KiB)
Jason Lightfoot avatar image Jason Lightfoot ♦♦ Ankur A3 commented ·
executestr(str1+"Value")

returns number. So you're then setting a number (eg. 5) to 80 - which doesn't make sense. You seem to want

executestring(str1+"Value=80");

But this looks all very poorly structured to me - it might serve you better to thing about the wealth of supported data structures and data types in FlexSim and choose something different (Maps, Arrays, Tables, Lists, Pointers).

0 Likes 0 ·
Ankur A3 avatar image Ankur A3 Jason Lightfoot ♦♦ commented ·

Hi @Jason Lightfoot,

Thanks for your answer!

Actually, I have series of global variables having some common text (Eg: Obj1_Mon_Utilization, Obj2_Tue_Utilization etc). I have to access all variables for n number of objects. So, I want to create a object loop for better scalability.

Would you like to suggest any other approach?

Thank you!

0 Likes 0 ·
Show more comments
Show more comments
Kavika F avatar image
0 Likes"
Kavika F answered Ankur A3 commented

Hey @Ankur A3, from what I understand you want to retrieve the value of a cell from your global table using code? If so, then you're close! Right now, you're assigning the cell the value "TableValue". That's because whatever is on the left of the "=" will be assigned the value of whatever is on the right of the "=". If you want to retrieve the value rather than assign it, put it on the left like so: 1654021918505.png

I set a variable "var value" equal to the result of the right side (whatever is in the global table at that cell). Now you can use that value later on in the code if you want. Did this answer your question?


1654021918505.png (40.9 KiB)
· 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.

Ankur A3 avatar image Ankur A3 commented ·

Hi @Kavika F,

I was asking same what @Matthew Gillespie answered.

Thanks for your support!

0 Likes 0 ·