Dynamic substitution of Variables in expressions

arun_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
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
0
Comments
-
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!0 -
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)0 -
Hi Luk & Kriki,
Thank you very much for your answers.
I was actually looking for something within navision.
Thanx again.
ARUN0 -
Hi Arun
I used Luc's idea and created a codeunit that looks like thisDocumentation() 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.
Albert0 -
Can you send it to me plz...
my mail id is: soumyadip AT rediffmail DOT com
Thank you0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions