cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Need help navigating or using the PTC Community? Contact the community team. X

Store a if/else expression inside a property or a table

SrijithKrish
12-Amethyst

Store a if/else expression inside a property or a table

Hi,

 

I have a quick query to be asked, I am creating a condition statement or a if/else condition dynamically and this statement is being stored in a property for testing purpose. I wanted to know how to use the expression stored inside the property for the if/else conditions because if I use the property directly it is evaluating if there is a value in the property and returning always true. But I want the condition inside the property to be evaluated.

Is there any solution for this purpose to use the dynamically created expressions as it is required?

 

Thanks in advance 

 

With Regards, 

Srijith

1 ACCEPTED SOLUTION

Accepted Solutions

I do not know if this fits your requirement or whether this is the best alternative. I am assuming left operand is always one of the properties of the thing. Please test properly. 

 

 

 

var exp = "me.prop < 10" ;

var operation = exp.split(" ");
var propValue = me[operation[0].split(".")[1]];

var result = false;
if(operation[1] == "<" && propValue < operation[2]){ // inlcude other operators using ||
    result = true;
}

 

 

View solution in original post

8 REPLIES 8

eval() would have worked but its usage should be avoided. Never use eval() 

Thank You @rjanardan 

 

But can you suggest anything other more efficient as you suggested that the use of eval() is not recommended.

It would be very nice to have a solution more efficient.

I can use eval() as a solution but as it is mentioned to reduce the use of eval() is there any other way I can solve the same issue other than eval().

 

I will have to use eval() if there are no other options but i am not sure about using eval() due to its defect.

 

Please let me know if there are any other solutions for the same purpose.

If using eval() cannot be avoided, then ensure that the string being passed to it cannot be modified to contain any malicious code. That is, you should scrutinize the usergroups that could write to this property. Although tedious, you could check if, splitting the string into operands and operators and then looping over all operators, is an option. 

'Splitting the string into operands and operators and then looping over all operators, is an option.'

Can you make this option a bit more clear? You mean that for eg I have a condition say : 

var result;

var exp = "me.prop < 10" ;

if(exp)

{

    result = "Success";

}

else

{

   result = "Unsuccessful";

}

 

Let's consider this example 

 

while using eval() I can do like :

var result;

var exp = "me.prop < 10" ;

if(eval(exp))

{

    result = "Success";

}

else

{

   result = "Unsuccessful";

}

 

Can you explain the tedious one just to make sure if I am right with I got from you? And if I can ensure that the string being passed to it cannot be modified to contain any malicious code which is better to use, the long way or this

I do not know if this fits your requirement or whether this is the best alternative. I am assuming left operand is always one of the properties of the thing. Please test properly. 

 

 

 

var exp = "me.prop < 10" ;

var operation = exp.split(" ");
var propValue = me[operation[0].split(".")[1]];

var result = false;
if(operation[1] == "<" && propValue < operation[2]){ // inlcude other operators using ||
    result = true;
}

 

 

Thank You @rjanardan 

 

I shall work with both and check which can be used as the best option and shalI consider the things u had asked me to follow while using eval().

 

With Regards,

Srijith

Top Tags