Options

Dynamic substitution of Variables in expressions

arun_mibusoarun_mibuso Member Posts: 14
Hi All,

Does anybody know how to do substitution of variables in string expressions.

E.g.

TXT = 'a*b + c*2'; // TXT is a string

a := 10;
b := 5;
c := 7;

d:= FUNCTION(TXT);

// is there any function which will substitute a, b & c of the string with the corresponding variables and evaluate the expression and set value of d to 64 [10*5 + 7*2]

Please help. Thanx in advance

ARUN

Comments

  • Options
    krikikriki Member, Moderator Posts: 9,090
    You might use STRSUBSTNO but not with a,b,c but with %1,%2,%3.

    And then use the DLL of MS Excel to calculate the result in Excel and get it back to Navision.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • Options
    Luc_VanDyckLuc_VanDyck Member, Moderator, Administrator Posts: 3,633
    You could have a look at codeunit 8 AccSchedManagement, function EvaluateExpression. I remember I used this Acc. Schedule functionality where you can define your Column Layouts with formula's, for a project where they needed some form of calculation.

    Using kriki's suggestion would be much easier (using Excel in the background to do the calculation).
    No support using PM or e-mail - Please use this forum. BC TechDays 2024: 13 & 14 June 2024, Antwerp (Belgium)
  • Options
    arun_mibusoarun_mibuso Member Posts: 14
    Hi Luk & Kriki,

    Thank you very much for your answers.
    I was actually looking for something within navision.

    Thanx again.
    ARUN
  • Options
    AlbertvhAlbertvh Member Posts: 516
    Hi Arun

    I used Luc's idea and created a codeunit that looks like this
    Documentation()
    
    OnRun()
    a := 10;
    b := 5;
    c := 7;
    Text := STRSUBSTNO('%1 * %2 + %3 * 2',a,b,c);
    d := PassText(Text);
    MESSAGE('Text   = %2\Result = %1',FORMAT(d),Text);
    
    PassText(PassedText : Text[250]) : Decimal
    Formula := PassedText;
    Exp := FALSE;
    IF (STRPOS(Formula,'+') > 0) OR
       (STRPOS(Formula,'-') > 0) OR
       (STRPOS(Formula,'*') > 0) OR
       (STRPOS(Formula,'^') > 0) OR
       (STRPOS(Formula,'/') > 0) THEN
      Exp := TRUE;
    EXIT(Calculate(Formula,Exp));
    
    Calculate(Expression : Text[250];PassIsExpression : Boolean) : Decimal
    Result := 0;
    Expression := DELCHR(Expression,'=',' ');
    IF STRLEN(Expression) > 0 THEN BEGIN
      Parantheses := 0;
      IsExpression := FALSE;
      Operators := '+-*/^';
      OperatorNo := 1;
      REPEAT
        i := STRLEN(Expression);
        REPEAT
          IF Expression[i] = '(' THEN
            Parantheses := Parantheses + 1
          ELSE
            IF Expression[i] = ')' THEN
              Parantheses := Parantheses - 1;
          IF (Parantheses = 0) AND (Expression[i] = Operators[OperatorNo]) THEN
            IsExpression := TRUE
          ELSE
            i := i - 1;
        UNTIL IsExpression OR (i <= 0);
        IF NOT IsExpression THEN
          OperatorNo := OperatorNo + 1;
      UNTIL (OperatorNo > STRLEN(Operators)) OR IsExpression;
      IF IsExpression THEN BEGIN
        IF i > 1 THEN
          LeftOperand := COPYSTR(Expression,1,i - 1)
        ELSE
          LeftOperand := '';
        IF i < STRLEN(Expression) THEN
          RightOperand := COPYSTR(Expression,i + 1)
        ELSE
          RightOperand := '';
        Operator := Expression[i];
        LeftResult :=
          Calculate(
            LeftOperand,FALSE);
        RightResult :=
          Calculate(
            RightOperand,FALSE);
        CASE Operator OF
          '^':
            Result := POWER(LeftResult,RightResult);
          '*':
            Result := LeftResult * RightResult;
          '/':
            IF RightResult = 0 THEN
              Result := 0
            ELSE
              Result := LeftResult / RightResult;
          '+':
            Result := LeftResult + RightResult;
          '-':
            Result := LeftResult - RightResult;
        END;
      END ELSE
        IF (Expression[1] = '(') AND (Expression[STRLEN(Expression)] = ')') THEN
          Result :=
            Calculate(COPYSTR(Expression,2,STRLEN(Expression) - 2),FALSE)
        ELSE
          EVALUATE(Result,Expression);
    END;
    EXIT(Result);
    
    

    You would of course delete the OnRun function and only run the codeunit as follows

    Text := STRSUBSTNO('%1 * %2 + %3 * 2',a,b,c);
    d := c.passtext(text);

    Maybe you could use this or if need be I can mail you the fob file.

    Albert
  • Options
    SoumyadipSoumyadip Member Posts: 209
    Can you send it to me plz...

    my mail id is: soumyadip AT rediffmail DOT com

    Thank you
Sign In or Register to comment.