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

Dynamic compare

0 Likes
929

Hi,

I want to compare two value like this.

data: opera(2) type c.

data: value1(15) type c.

data: value2(15) type c.

if value1 opera value2.

true.

else.

false.

endif.

i have try the marco statment. but it does not support realtional operator. Any idea ?

Thanks

Wilson

6 REPLIES 6
Read only

jayanthi_jayaraman
Active Contributor
0 Likes
842

Hi,

You can use wildcard.

DATA: f1(5) TYPE c VALUE <f1>,

f2(5) TYPE c VALUE <f2>.

IF f1 <operator> f2.

WRITE: / 'Vergleich wahr, SY-FDPOS=', sy-fdpos.

ELSE.

WRITE: / 'Vergleich falsch, SY-FDPOS=', sy-fdpos.

ENDIF.

The following table shows the results of executing this program, depending on which operators and values of F1 and F2.

<f1> <operator> <f2> Result SY-FDPOS

'BD ' CO 'ABCD ' true 5

'BD ' CO 'ABCDE' false 2

'ABC12' CN 'ABCD ' true 3

'ABABC' CN 'ABCD ' false 5

'ABcde' CA 'Bd ' true 1

'ABcde' CA 'bD ' false 5

'ABAB ' NA 'AB ' false 0

'ababa' NA 'AB ' true 5

'ABcde' CS 'bC ' true 1

'ABcde' CS 'ce ' false 5

'ABcde' NS 'bC ' false 1

'ABcde' NS 'ce ' true 5

'ABcde' CP 'b' true 1

'ABcde' CP '#b' false 5

'ABcde' NP 'b' false 1

'ABcde' NP '#b' true 5

Hope this helps.If so,kindly reward points and close the thread.

Read only

0 Likes
842

Sorry, I want to compare two values.

data: opera(2) type c.

data: value1 type I.

data: value2 type I.

value1 = 16.

value2 = 18.

opera = '>='.

if value1 opera value2.

true.

else.

false.

endif.

Thanks

Wilson

Read only

0 Likes
842

Hi, try to use GENERATE SUBROUTINE POOL:

...

APPEND 'report z1.' TO progtab.
APPEND 'form upro using result.' TO progtab.
CONCATENATE 'if' value1 opera value2 '.' INTO progtab
              SEPARATED BY space.
APPEND progtab.
APPEND 'result = 1.' TO progtab.
APPEND 'endif.' TO progtab.
APPEND 'endform.' TO progtab.
*
GENERATE SUBROUTINE POOL progtab NAME genprog MESSAGE message
                                           LINE line WORD word.
IF sy-subrc <> 0.
message e001(00) with message line word.
ENDIF.
*
PERFORM upro IN PROGRAM (genprog) USING result.

Andreas

Read only

Former Member
0 Likes
842

Hi, Wilson

using GENERATE SUBROUTINE POOL might meet your requirement.

But are you sure you really need to import it into your code?

It shouldn't do good at performance.

Just do a case-when logic filter, don't make the code too complex. It's my suggestion.

Read only

Former Member
0 Likes
842

Hi Wilson,

I have never come across a requirement like this. I hope it is just a matter of academic interest for you. In case you're trying to use this in a program for your customer, then I must warn you that trying to determine the operator at run time will make debugging and error-detection very difficult. Also, I don't think it is a nice way of programming your logic.

Now, here's what I think you can do -


define evaluate_condition.
  case &2.
    when 1.
      if &1 EQ &3.
      "  do something.
      endif.
    when 2.
      if &1 NE &3.
      "  do something.
      endif.    
    when 3.
      if &1 GT &3.
      "  do something.
      endif.    
    when 4.
      if &1 GE &3.
      "  do something.
      endif.    
    when 5.
      if &1 LT &3.
      "  do something.
      endif.    
    when 6.
      if &1 LE &3.
      "  do something.
      endif.  
  endcase.
end-of-definition.


constants: eq type n value '1',
           ne type n value '2',
           gt type n value '3',
           ge type n value '4',
           lt type n value '5',
           le type n value '6'.
           

data: operand1 type i,
      operator type n,
      operand2 type i.

operand1 = 16.
operand2 = 18.
operator = ge.

evaluate_condition operand1 operator operand2.

operand1 = 10.
operand2 = 10.

evaluate_condition operand1 eq operand2.

You might get the feeling that this code is not really dynamic. I agree. But I think it is readable and will make life easier for anybody else who reads / reviews your program :-).

Let me know what you think. And reward the points by clicking the stars if any of the answers have helped you.

Regards,

Anand Mandalika.

Read only

0 Likes
842

Thanks all !!

I will use this method.

data: it_temp like range of bsid-wrbtr with header line.

it_temp-low = value2.

it_temp-sign = 'I'.

it_temp-option = opera.

loop at wa_temp

where value1 in it_temp.

true.

endloop.

Any comments ?

Thanks

Wilson