Application Development 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: 

Calculate a result in a string

Former Member
0 Kudos

I have string, which contains '5 * 7'.

How can I get the result into my integer variable?

Any ideas.

Thanks

Rene

1 ACCEPTED SOLUTION

Former Member
0 Kudos

If all you know is that there is a valid computation in the string, you could use the "GENERATE" statement to put it into a program and execute it.

Rob

11 REPLIES 11

suresh_datti
Active Contributor
0 Kudos

try this


data : w_result type i,
       w_string1 type string,
       w_string2 type string,
       w_string3 type string.

w_string1 = '5*7'.
split w_string1 at '*' into w_string2 w_string3.
w_result = w_string2 * w_string3.

Regards,

Suresh Datti

Former Member
0 Kudos

If you know that the * character is the seperating character you can use this..

DATA str(12) VALUE '1 * 3'.
DATA str1(10).
DATA str2(10).

SPLIT str AT '*' into str1 str2.

DATA i_1 TYPE i.
DATA i_2 TYPE i.

i_1 = str1.
i_2 = str2.

WRITE: i_1, i_2.

Former Member
0 Kudos

If all you know is that there is a valid computation in the string, you could use the "GENERATE" statement to put it into a program and execute it.

Rob

0 Kudos

You could do this sort of thing:

REPORT znrw_calculator MESSAGE-ID z1.

PARAMETERS: p_code1(20) LOWER CASE.

DATA t_source_tab(72) OCCURS 0 WITH HEADER LINE.

DATA g_program_name LIKE sy-repid.

DATA g_syntax_message(128).

DATA g_line_no TYPE i.

DATA var TYPE p.

AT SELECTION-SCREEN.

PERFORM check_dynamic_abap.

START-OF-SELECTION.

PERFORM do_the_business IN PROGRAM (g_program_name) CHANGING var .

WRITE:/ p_code1 NO-GAP,'=' NO-GAP,var.

&----


*& Form CHECK_DYNAMIC_ABAP

&----


FORM check_dynamic_abap.

DATA t_abap(80) OCCURS 0 WITH HEADER LINE.

*set up a dynamic program

REFRESH t_source_tab.

APPEND 'REPORT' TO t_source_tab.

APPEND sy-repid TO t_source_tab.

APPEND '.' TO t_source_tab.

APPEND 'FORM do_the_business changing var type p.' TO t_source_tab.

APPEND 'var =' TO t_source_tab.

append p_code1 to t_source_tab.

APPEND '.' TO t_source_tab.

APPEND 'ENDFORM.' TO t_source_tab.

*Generate the dynamic program so that the form can be used subsequently.

GENERATE SUBROUTINE POOL t_source_tab NAME g_program_name

MESSAGE g_syntax_message LINE g_line_no.

IF sy-subrc <> 0.

MESSAGE e999 WITH g_syntax_message.

ENDIF.

ENDFORM. . "CHECK_DYNAMIC_ABAP

0 Kudos

Hi

I am in the processs of writting my own code to evaluate the string, if it is any type of formular with + - / * and ( ) .

But I have also looked in to the generate subroutine pool solutions as your are proposing.

I was looking for something better then these two options.

Have you an idea how fast the generate subroutine pool solution would be.

I have to do this for a lot of records and need a very fast solution.

Thanks

0 Kudos

If all of your records can be generated at once in one subroutine pool, it shouldn't be too bad; but if you have to generate a separate pool for each statement, it would be quicker to parse it yourself (although it mnight be quite a programming challenge).

Rob

0 Kudos

the generation method is very elegant but is really best suited to an on-line validation of a free-from string. If you have thousands of records I think the best solution will be the long-winded approach, this will give you better error handling capability too.

Former Member
0 Kudos

HI rene,

1. There is no direct way

to do such computations

which are available as a string.

2. by doing some hard coding,

we can manage to calculatge 5 * 7.

3. But if the formula is a little more

complex

eg. 2 *5 + (6 / 3 ) * 5.5 - 9

then, things go for a toss !

regards,

amit m.

Former Member
0 Kudos

Rene,

Here is some example logic from the suggestions stated above, and more complex, also stated above, computations get interesting. Good luck.

Regards,

Terry


REPORT Y_CALCULATE_STRING_RESULT .

DATA: MYSTRING(30),
      VALUE1(10),
      VALUE2(10),
      RESULT(10),
      CALC_ADD VALUE '+',
      CALC_SUBTRACT VALUE '-',
      CALC_MULTIPLY VALUE '*',
      CALC_DIVIDE VALUE '/'.

MYSTRING = '5 * 7'.
*MYSTRING = '5 + 7'.
*MYSTRING = '5 - 7'.
*MYSTRING = '5 / 7'.

SPLIT MYSTRING AT CALC_ADD INTO VALUE1 VALUE2.

IF NOT VALUE2 IS INITIAL.
  CONDENSE: VALUE1, VALUE2.
  RESULT = VALUE1 + VALUE2.
  CONDENSE RESULT.
ENDIF.

SPLIT MYSTRING AT CALC_SUBTRACT INTO VALUE1 VALUE2.

IF NOT VALUE2 IS INITIAL.
  CONDENSE: VALUE1, VALUE2.
  RESULT = VALUE1 - VALUE2.
  CONDENSE RESULT.
ENDIF.

SPLIT MYSTRING AT CALC_MULTIPLY INTO VALUE1 VALUE2.

IF NOT VALUE2 IS INITIAL.
  CONDENSE: VALUE1, VALUE2.
  RESULT = VALUE1 * VALUE2.
  CONDENSE RESULT.
ENDIF.

SPLIT MYSTRING AT CALC_DIVIDE INTO VALUE1 VALUE2.

IF NOT VALUE2 IS INITIAL.
  CONDENSE: VALUE1, VALUE2.
  RESULT = VALUE1 / VALUE2.
  CONDENSE RESULT.
ENDIF.

WRITE:/ 'The result of', MYSTRING,
      / 'is', RESULT.

Former Member
0 Kudos

I have done tests in both directions. With my own code and generated code. The own code is clearly faster if you need to generate serveral performs.

0 Kudos

Another option would be to use a macro.

eg look at this program.

REPORT ZMACRO.

DATA: RESULT TYPE I,

N1 TYPE I VALUE 5,

N2 TYPE I VALUE 6.

DEFINE OPERATION.

RESULT = &1 &2 &3.

OUTPUT &1 &2 &3 RESULT.

END-OF-DEFINITION.

DEFINE OUTPUT.

WRITE: / 'The result of &1 &2 &3 is', &4.

END-OF-DEFINITION.

OPERATION 4 + 3.

OPERATION 2 ** 7.

OPERATION N2 - N1.

The produces the following output:

The result of 4 + 3 is 7

The result of 2 ** 7 is 128

The result of N2 - N1 is 1