0

I'm trying to compare a array of elements to one value, is there any way to do that using ncalc?

Example:

new Expression(ruleExpression.Replace(" [1,2,3] > 1 and 2 < 3 and 2 == 2").Evaluate(); 

The result that I want is FALSE to ([1,2,3] > 1 ), because not all elements respect the rule.

Mighty Badaboom
  • 6,067
  • 5
  • 34
  • 51
  • Could you do a `Min` on the array and then compare to 1? – mjwills Oct 06 '17 at 12:04
  • Thanks to reply. But, I need something more dinamic. If a use the operator MIN in this case ([1,2,3] == 1), it will not work. – Matheus Castro Oct 06 '17 at 12:10
  • For equality, you could use both `Min` and `Max`. If both `Min` and `Max` are equal to 1, then they are all equal to 1. – mjwills Oct 06 '17 at 12:18
  • Your Idea is good and valid. But do you know a simple way do detect the operator before evaluate it ? – Matheus Castro Oct 06 '17 at 12:33
  • You can compile the expression (instead of evaluating it). – Caramiriel Oct 06 '17 at 12:38
  • @Caramiriel can you explain it better ? – Matheus Castro Oct 06 '17 at 12:40
  • @Matheus in a nutshell: if you would use `.Compile()`, you would get a tree-like representation of the expression (ie: https://caml.inria.fr/pub/docs/oreilly-book/html/book-ora022.gif). If you would pass that expression into an implemented `NCalc.Domain.LogicalExpressionVisitor`, then you'd be able to 'see' the `Min`/`Max` functions and do things with it that you want. – Caramiriel Oct 06 '17 at 12:59
  • @Caramiriel do you know a method that return all the expression parts without in a "tree" way ? – Matheus Castro Oct 06 '17 at 14:24

1 Answers1

0

This is a slightly later answer but I thought it still worth answering in case others stumbled across the same issue.

If you are not against migrating away from NCalc then I have created an alternative expression evaluation framework that was originally built to match NCalcs behaviour. It is called Expressive and is available on GitHub or NuGet.

While you can't yet write arrays directly in expressions you can supply them as variables. So to adapt your expression slightly you can supply the following and get the result you are expecting:

new Expression("[array] > 1 and 2 < 3 and 2 == 2").Evaluate(new Dictionary<string, object>()
{
    ["array"] = new int[] {1,2,3}
});
Bijington
  • 3,661
  • 5
  • 35
  • 52