Recent

Author Topic: Using string array elements as operators  (Read 827 times)

mstefa

  • Newbie
  • Posts: 2
Using string array elements as operators
« on: December 16, 2019, 08:27:03 am »
I have an array full of operators ( <,>, ==.. ) of type string.
How can I use them to evaluate members from other arrays of integers or reals..
I really need to be able to compare first and second ( or more) elements from integer array using operators stored in string array.
Code snippet, pseudo code, just a hint.. any help will be appreciated.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Using string array elements as operators
« Reply #1 on: December 16, 2019, 10:50:19 am »
You can use symbolic unit to evaluate the expression.
You can use string representations of an equation there.
Might be a little overkill.

You could imagine having a function like this:
Code: Pascal  [Select][+][-]
  1. function ApplyOperator(const L, R: Integer; const Op: string): Integer;
  2. begin
  3.   case Op of
  4.     '+': Result := L+R;
  5.     '-': Result := L-R;
  6.     '*': Result := L*R;
  7.     '**': Result := L**R;
  8.   end;

You can the do something like:
Code: Pascal  [Select][+][-]
  1. var
  2.   IntArr1, IntArr2: Array of Integer;
  3.   OpArr: Array of String;
  4. ....
  5.   Res := ApplyOperator(IntArr1[0], IntArr2[0], OpArr[3]);

You can have a similar function for logical operators which would return a boolean.

Bart

mstefa

  • Newbie
  • Posts: 2
Re: Using string array elements as operators
« Reply #2 on: December 17, 2019, 10:53:01 pm »
Thank you very much. THat helped a lot.

 

TinyPortal © 2005-2018