Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Process a formula that is in an string

Former Member
0 Likes
859

Hi,

does anybody know how to process a formula that comes in an string?

Por ejemplo:

a = 1.

b = 2.

c = 3.

string = '( a + b ) * c'.

....

string = '( 1 + 2 ) * 3'.

how can I get the result of that formula.

Thanks in advance.

1 ACCEPTED SOLUTION
Read only

naimesh_patel
Active Contributor
0 Likes
454

You can try to generate a Dynamic Subroutine-pool and call it from the your program.

Check this sample code:


DATA: l_string TYPE string,
      a TYPE i,
      b TYPE i,
      c TYPE i,
      res TYPE i.

DATA: l_source TYPE STANDARD TABLE OF char72 WITH HEADER LINE.

a = 1.
b = 2.
c = 3.

FIELD-SYMBOLS: <fs> TYPE ANY.

l_string = '( A + B ) * C'.


l_source = 'PROGRAM ZTEST_TMP_123.'.
APPEND l_source.
l_source = 'form test using a b c changing res.'.
APPEND l_source.

l_source = 'RES = '.
APPEND l_source.

l_source = l_string.
APPEND l_source.

l_source = '.'.
APPEND l_source.

l_source = 'endform.'.
APPEND l_source.

DATA: prog TYPE string,
      tab  TYPE STANDARD TABLE OF string,
      mess TYPE string,
      sid  TYPE string.

GENERATE SUBROUTINE POOL l_source NAME prog
         MESSAGE mess
         SHORTDUMP-ID sid.

IF sy-subrc = 0.
  PERFORM ('TEST') IN PROGRAM (prog) IF FOUND
  USING a b c CHANGING res .
ELSEIF sy-subrc = 4.
  MESSAGE mess TYPE 'I'.
ELSEIF sy-subrc = 8.
  MESSAGE sid TYPE 'I'.
ENDIF.

WRITE: l_string, '=', res.

Regards,

Naimesh Patel

2 REPLIES 2
Read only

naimesh_patel
Active Contributor
0 Likes
455

You can try to generate a Dynamic Subroutine-pool and call it from the your program.

Check this sample code:


DATA: l_string TYPE string,
      a TYPE i,
      b TYPE i,
      c TYPE i,
      res TYPE i.

DATA: l_source TYPE STANDARD TABLE OF char72 WITH HEADER LINE.

a = 1.
b = 2.
c = 3.

FIELD-SYMBOLS: <fs> TYPE ANY.

l_string = '( A + B ) * C'.


l_source = 'PROGRAM ZTEST_TMP_123.'.
APPEND l_source.
l_source = 'form test using a b c changing res.'.
APPEND l_source.

l_source = 'RES = '.
APPEND l_source.

l_source = l_string.
APPEND l_source.

l_source = '.'.
APPEND l_source.

l_source = 'endform.'.
APPEND l_source.

DATA: prog TYPE string,
      tab  TYPE STANDARD TABLE OF string,
      mess TYPE string,
      sid  TYPE string.

GENERATE SUBROUTINE POOL l_source NAME prog
         MESSAGE mess
         SHORTDUMP-ID sid.

IF sy-subrc = 0.
  PERFORM ('TEST') IN PROGRAM (prog) IF FOUND
  USING a b c CHANGING res .
ELSEIF sy-subrc = 4.
  MESSAGE mess TYPE 'I'.
ELSEIF sy-subrc = 8.
  MESSAGE sid TYPE 'I'.
ENDIF.

WRITE: l_string, '=', res.

Regards,

Naimesh Patel

Read only

0 Likes
454

Thanks very much Naimesh for your solution