‎2011 Jul 07 11:58 AM
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
‎2011 Jul 07 12:07 PM
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.
‎2011 Jul 07 12:21 PM
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.
‎2011 Jul 07 3:22 PM
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.
‎2011 Jul 07 5:27 PM
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
‎2011 Jul 08 7:54 AM
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.
‎2011 Jul 07 11:10 PM
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
‎2011 Jul 08 3:09 PM
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.