Skip to Content
author's profile photo Former Member
Former Member

Calculate a result in a string

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

How can I get the result into my integer variable?

Any ideas.

Thanks

Rene

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

6 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Mar 31, 2006 at 03:56 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Mar 31, 2006 at 03:06 PM

    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.
    

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Mar 31, 2006 at 03:24 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Mar 31, 2006 at 03:25 AM

    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.
    

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Mar 31, 2006 at 04:45 AM

    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.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 29, 2008 at 09:49 PM

    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.

    Add a comment
    10|10000 characters needed characters exceeded

    • 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

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.