Reading a string table element as a variable name

Hello,

I have an application where I want to keep a string table where each element contains a string that is the name of a variable in my strategy. Let’s call, this table ‘stVarNames’ and has the following structure:
[Index]:[Element]
0: ‘Var1’
1:‘Var2’
2:‘Var3’

I want a table like this because, depending on the particular day of operation, I will be interested in some subset of my variables. Now I have a float table called ‘ftVarSetPoints’ which contains the values I want the variables named in stVarNames to take during my strategy run. ftVarSetPoints will change over time so I want to update ftVarSetPoints rather than directly setting the values via a manual input. ftVarSetPoints would look like this:
[Index]:[Element]
0:1.0
1:2.0
2:3.0

My intention is to build a chart that will assign a particular strategy variable found in stVarNames with the value corresponding to the same table index of ftVarSetPoints.

So if my strategy has float variables called ‘Var1’, ‘Var2’, and ‘Var3’ this chart will programmatically assign their values as:
Var1=1.0
Var2=2.0
Var3=3.0

Then, if ftVarSetPoints changes in another loop of the chart, the three variables would be modified appropriately. Finally, if at another point stVarNames has different variable names as elements, the appropriate variables will be assigned.

I have successfully used GetValueFromName to go the reverse route, where the table of variable names is used to programmatically obtain the variable value to then save in a csv file. In this scenario I don’t think GetValueFromName will work because I want to assign the variable value, not look up the current one.

I looked through the forums and it sounds like pointer tables may work for what I want to achieve as discussed here, but I have never used them so I am not entirely sure. The user guide cautions on the use of pointers due to potential debugging and errors, so I wanted to get feedback on what could be potential approaches to achieve my objective.

Thanks for any input on this topic!
Juan

Here is a general outline for doing this.

  • Parse the variable name and the value. Trim spaces or CR LF characters off the ends of the strings.

  • GetTypeFromName(PassedVarName); to get the data type

  • Based on the type of point, GetPointerFromName(PassedVarName, pInt32); [ lets assume it is an integer32 variable type ]
    make sure you use the proper pointer variable type to match up with the actual data type.
    Use a Switch or a long if…then…else based on the data type.

  • Convert the value part of the string into the data type value. iVal = StringToInt32(PassedValueAsStr);

  • Set the Value. *pInt32 = iVal;

3 Likes

Sounds similar to a routine I have recently developed where I bring recipe details in from a MSSQL database and post them to various tables.

Example:

  1. Recipe Name (string table)
  2. Target (float table)

Table length of 800 elements.

The method I use is more than likely not best practice and uses a little bit of processing power however it works for me.

So the desired recipe name is presented in a string variable, I then loop through the the recipe table using a while loop to find a match for the recipe name, maintaining an index variable (i), and incrementing it respectively while searching. Once established the index count is stored and I use that as my reference to then retrieve the other relevant data for that recipe from the other data tables and placing them in the respective variables for processing.

Recipe (string table)
0: ‘Jam’
1:‘Peanut ’
2:‘Orange’

strvar to lookup “Peanut”, after finding the match “peanut==peanut” my index will be “i=1” respectively.

Quantity: (float table)
0: 2.5
1:8.6
2:9.8

curQuantity = Quantity[i]

Hope this makes sense and as mentioned it works for me.

Manage a recipe database of over 500 recipes of 15 elements each with this method. Naturally the initial lookup for the recipe name is key as the operator would have to capture this perfectly. This I found is something they cannot get right and therefore developed a lookup facility where they simply select the name to avoid typo’s

Memory constraints should come into consideration aswell for massive tables if storing as persistent. Saying that the current available memory in the PR1 can handle a significant amount of data though.

Regards

2 Likes

Thanks for you reply!

A couple of clarifications on your approach:

Does the GetTypeFromName command require PassedVarName to be a literal as suggested in the command reference, or can it read a string variable? I am not sure how I would send it a literal instead of a string variable.

For the pointer data type, do you think I would need to initialize separate pointer variables for each data type as in pInt32, pFloat, etc and then select one or the other via the switch of if statement you suggested? Or can I change the data type of a single pointer variable programmatically?

Thanks again for your feedback!

Thanks for your suggestion!

If I understood your recipe approach correctly, a key aspect of it is that the indices of all tables match right? Or am I missing something about how ‘Peanut’ tells the chart to read a specific index of your float table? Seems to me that great care must be taken to make sure there is no index mismatch over time. Do you fill the recipe and float tables via the database and then break them into separate tables within Opto to achieve this?

Thanks for your feedback!

Correct,

All Recipe 1 components will be at element 0,
All Recipe 2 components will be at element 1,

of all tables.

Regards