 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

10|10000 characters needed characters exceeded

### Related questions 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

10|10000 characters needed characters exceeded
• Neil Woodruff Former Member

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
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_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.
```

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

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.
```

10|10000 characters needed characters exceeded
• 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.

10|10000 characters needed characters exceeded
• 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.

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