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

Logical Expression

Former Member
0 Likes
2,807

Hi,

I have an internal table contains field for logical expression EQ, GE, GT etc.

I would like to use this logical expression in my ABAP code.

For example

READ TABLE INTAB INDEX 1.

Comparing the value of a and b based on the logical expression from the INTAB.

How can i achieve this ?

Thanks.

Regards,

Jon

7 REPLIES 7
Read only

Former Member
0 Likes
1,245

Hi,

I doubt if we can pass a variable in place of logical expression.

You can try by reading your internal table, and having various if conditions.But that doesnt look like a good solution.

Read only

former_member585060
Active Contributor
0 Likes
1,245

Hi,

Make use of macros, just check below example.

DATA : lv_abc  TYPE i VALUE 5,
       lv_bca  TYPE i VALUE 3.


DEFINE mac.

  if lv_abc &1 lv_bca.
    write :/ 'Bala'.
  else.
    write :/'Krishna'.
  endif.

END-OF-DEFINITION.

mac gt.

In above code, you can pass the INTAB table field value with the defined macro.

Ex:-

mac intab-field.

Regards

Bala Krishna.

Read only

Former Member
0 Likes
1,245

Yes, i have try not to use if and else condition to fulfill the requirement.

Hi Bala,

Thanks for providing example, however i'm not really get it on how to use it.

I will provide more details scenario

Table ZTAB consist of following structure and records

abc GT

cef EQ

a = 50.

b = 100.

READ TABLE ztab with key field = 'abc'.

Based on the table entries from ztab, i sud use the operation GT to compare between value a and b.

so instead of using this way

IF ztab-sign = 'GT'.

IF a GT b.

WRITE: 'Successful'

ENDIF.

ENDIF.

Is there a way to define the operation based on the table entries ?

Thanks.

Read only

0 Likes
1,245

Hi,

you could use an approach populating a RANGE.

For example:


DATA: BEGIN OF ztab OCCURS 0,
        field(10) TYPE c,
        operator(2) TYPE c,
      END OF ztab.

RANGES: r_field FOR sbfadxth-keychar10. "any DDIC field with the same
                                        "type as ZTAB-FIELD

INITIALIZATION.
* Just initializing the example you provided
  ztab-field = 'abc'.
  ztab-operator = 'EQ'.
  APPEND ztab.

  ztab-field = 'cef'.
  ztab-operator = 'GT'.
  APPEND ztab.

START-OF-SELECTION.
  READ TABLE ztab WITH KEY field = 'abc'.
  IF sy-subrc = 0.
*   Now you'll fill the range according to the entry you've just READ
    CLEAR: r_field.
    REFRESH: r_field.

    r_field-sign = 'I'.
    r_field-option = ztab-operator.
    r_field-low = ztab-field.
    r_field-high = space.
    APPEND r_field.

*   Finally you make the comparison
    IF a IN r_field.
      WRITE: 'Successful'.
    ENDIF.

  ENDIF.

I hope this helps. Kind regards,

Alvaro

Read only

0 Likes
1,245

Hi,

The required code has to be declared in the Macro defination, it is define between the statement DEFINE and END-OF-DEFINATION, with the help of CASE statement you can code for all the possible Options. Just check the below example.

DATA : v_aaa TYPE i VALUE 50,
       v_bbb TYPE i VALUE 100.

TYPES : BEGIN OF ty_ztab,
          field  TYPE c LENGTH 5,
          option TYPE c LENGTH 2,
        END OF ty_ztab.

DATA : i_ztab  TYPE TABLE OF ty_ztab,
       wa_ztab TYPE ty_ztab.


DEFINE macro.

  case &1.
    when 'GT'.

      if v_aaa gt v_bbb.
        write :/ v_aaa, 'Greater than', v_bbb.
      else.
        write :/ v_aaa, 'Less than', v_bbb.
      endif.

    when 'EQ'.

      if v_aaa eq v_bbb.
        write :/ 'Equal'.
      endif.

    when 'LT'.

    when 'NE'.

    when others.
  endcase.

END-OF-DEFINITION.


START-OF-SELECTION.

  wa_ztab-field = 'abc'.
  wa_ztab-option = 'GT'.
  APPEND wa_ztab TO i_ztab.
  CLEAR : wa_ztab.

  wa_ztab-field = 'cef'.
  wa_ztab-option = 'EQ'.
  APPEND wa_ztab TO i_ztab.
  CLEAR : wa_ztab.

  READ TABLE i_ztab INTO wa_ztab WITH KEY
                    field = 'abc'.
  IF sy-subrc = 0.
    macro wa_ztab-option.
  ENDIF.

Output will be :- 50 Less than 100, which is written in ELSE condition of the CASE condition GT.

Regards

Bala Krishna.

Read only

Clemenss
Active Contributor
0 Likes
1,245

Hi O. SITI EPBP,

you could create form routines for the operators and call them dynamically.

PERFORM (ztab-sign) using a b changing result.
WRITE: / a,ztab-sign,b,'is',result.

FORM GT using p_val_a p_val_b changing p_result.
  IF  p_val_a  > p_val_b .
    p_result. = 'TRUE'.
  ELSE.
    p_result. = 'FALSE'.
  ENDIF.
ENDFORM.
FORM LT using p_val_a p_val_b changing p_result.
  IF  p_val_a  < p_val_b .
    p_result. = 'TRUE'.
  ELSE.
    p_result. = 'FALSE'.
  ENDIF.
ENDFORM.

Create one form for each operator.

Regards,

Clemens

Read only

Former Member
0 Likes
1,245

Hi Alvaro ,

Thanks for the solution. Point Rewarded.

Hi Bala,

Your solution will work too, however i try to eliminate using too much IF and ENDIF statement for this requirement.

But thanks for your details explanation.