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

Problem with if checking

Former Member
0 Likes
1,982

I am encountering with a strange problem. I am checking for a relational operator after a 'IF' field. for eg

if a correct.

a jh b -


> incorrect ( because jh is not a relational operator)

My code is

if not itab-line CA '=,,<,>,<=,>=,eq,ne,lt,gt,le,ge,co,cn,ca,na,cs,ns,cp,np,between,is,in,not'.

RAISE INVALID_IF.

endif.

This code was working fine (raising exception) when I test it with special characters ( !,@,# etc ) but as soon as I tested it with characters it is passing all the test without raising exception for eg

a gth b, c ytl 100 --> it should ideally raise exception as gth,ytl are not relational operator....but still it is passing all the test.

What should I use instead of CA??

Edited by: priya singh on Jan 16, 2009 1:08 PM

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,403
CA '=,,<,>,<=,>=,eq,ne,lt,gt,le,ge,co,cn,ca,na,cs,ns,cp,np,between,is,in,not'

You're making the mistake to assume each operator in '=,,<,>,<=,>=,eq,ne,lt,gt,le,ge,co,cn,ca,na,cs,ns,cp,np,between,is,in,not' is validated, but it's not.

However, each separate character in that string is checked

So if your string would contain a single character from '=,,<,>,<=,>=,eq,ne,lt,gt,le,ge,co,cn,ca,na,cs,ns,cp,np,between,is,in,not' the check will pass your test as well.

You can test it out yourself:

data: lv_test1 type string,
      lv_test2 type string.


lv_test1 = '>>'.
lv_test2 = 'gtttt'.

if lv_test1 ca '>,gt'.
  write:/ 'contains operator'.
else.
  write:/ 'does not contain operator'.
endif.


if lv_test2 ca '>,gt'.
  write:/ 'contains operator'.
else.
  write:/ 'does not contain operator'.
endif.

So i don't think you will be able to do your check with CO CP CS and such.

A possible solution might be to create an internal table with each operator in an entry.;And then to check if you string holds one of the entries in your internal table.

Just an example:

types: begin of ty_operators,
          value type string,
       end of ty_operators,

       begin of ty_split,
         value(30) type c,
       end of ty_split.

data: gv_text1(30) type c,
      gv_text2(30) type c,
      it_operators type table of ty_operators,
      wa_operators type ty_operators,
      it_split     type table of ty_split,
      wa_split     type ty_split.


clear: it_operators[].

wa_operators-value = '='.  append wa_operators to it_operators.
wa_operators-value = '<'.  append wa_operators to it_operators.
wa_operators-value = '>'.  append wa_operators to it_operators.
wa_operators-value = '<='. append wa_operators to it_operators.
wa_operators-value = '>='. append wa_operators to it_operators.
wa_operators-value = 'eq'. append wa_operators to it_operators.
* and so on for all your operators


gv_text1 = 'IF >>bla eq blabla'.
split gv_text1 at space into table it_split.

loop at it_split into wa_split.

  read table it_operators into wa_operators with key value = wa_split-value.
  if sy-subrc eq 0.
     write: / gv_text1, 'contains operator', wa_operators-value.
  endif.

endloop.



gv_text2 = 'IF bla eqeq blabla'.
split gv_text2 at space into table it_split.

loop at it_split into wa_split.

  read table it_operators into wa_operators with key value = wa_split-value.
  if sy-subrc eq 0.
     write: / gv_text2, 'contains operator', wa_operators-value.
  endif.

endloop.

Maybe there are other solutions, but you just have to try harder to find them.

7 REPLIES 7
Read only

tarangini_katta
Active Contributor
0 Likes
1,403

Hi priya,

Use contanis pattren 'CP'.

it may solve u r pblm.

Thanks,

Read only

Former Member
0 Likes
1,403

Why dont you use

if not itab-line CN '=,,<,>,<=,>=,eq,ne,lt,gt,le,ge,co,cn,ca,na,cs,ns,cp,np,between,is,in,not'.

RAISE INVALID_IF.

endif.

Instead of

if not itab-line CA '=,,<,>,<=,>=,eq,ne,lt,gt,le,ge,co,cn,ca,na,cs,ns,cp,np,between,is,in,not'.

RAISE INVALID_IF.

endif.

Try it.

Regards,

Madan Mohan.

Read only

SujeetMishra
Active Contributor
0 Likes
1,403

Hello Priya,

These !,@,# relational operator is Exception in SAP.

you can simply check this by writing a program for validation, your program will never work for these.

Infact I was searching its answer since long time but did not get any result and conclusion. if you get then please update this thread.

Have a Nice Day,

Regards,

Sujeet

Read only

Former Member
0 Likes
1,403

Hi ,

The comparison operators takes into account evn the leading and triling blank values when comparision.

Contains Any: True, if operand1 contains at least one character from operand2.

Please refer the below link and choose a suitable operator:

http://help.sap.com/saphelp_nw70/helpdata/en/fc/eb3516358411d1829f0000e829fbfe/frameset.htm

Regards,

Radhika.

Read only

Former Member
0 Likes
1,404
CA '=,,<,>,<=,>=,eq,ne,lt,gt,le,ge,co,cn,ca,na,cs,ns,cp,np,between,is,in,not'

You're making the mistake to assume each operator in '=,,<,>,<=,>=,eq,ne,lt,gt,le,ge,co,cn,ca,na,cs,ns,cp,np,between,is,in,not' is validated, but it's not.

However, each separate character in that string is checked

So if your string would contain a single character from '=,,<,>,<=,>=,eq,ne,lt,gt,le,ge,co,cn,ca,na,cs,ns,cp,np,between,is,in,not' the check will pass your test as well.

You can test it out yourself:

data: lv_test1 type string,
      lv_test2 type string.


lv_test1 = '>>'.
lv_test2 = 'gtttt'.

if lv_test1 ca '>,gt'.
  write:/ 'contains operator'.
else.
  write:/ 'does not contain operator'.
endif.


if lv_test2 ca '>,gt'.
  write:/ 'contains operator'.
else.
  write:/ 'does not contain operator'.
endif.

So i don't think you will be able to do your check with CO CP CS and such.

A possible solution might be to create an internal table with each operator in an entry.;And then to check if you string holds one of the entries in your internal table.

Just an example:

types: begin of ty_operators,
          value type string,
       end of ty_operators,

       begin of ty_split,
         value(30) type c,
       end of ty_split.

data: gv_text1(30) type c,
      gv_text2(30) type c,
      it_operators type table of ty_operators,
      wa_operators type ty_operators,
      it_split     type table of ty_split,
      wa_split     type ty_split.


clear: it_operators[].

wa_operators-value = '='.  append wa_operators to it_operators.
wa_operators-value = '<'.  append wa_operators to it_operators.
wa_operators-value = '>'.  append wa_operators to it_operators.
wa_operators-value = '<='. append wa_operators to it_operators.
wa_operators-value = '>='. append wa_operators to it_operators.
wa_operators-value = 'eq'. append wa_operators to it_operators.
* and so on for all your operators


gv_text1 = 'IF >>bla eq blabla'.
split gv_text1 at space into table it_split.

loop at it_split into wa_split.

  read table it_operators into wa_operators with key value = wa_split-value.
  if sy-subrc eq 0.
     write: / gv_text1, 'contains operator', wa_operators-value.
  endif.

endloop.



gv_text2 = 'IF bla eqeq blabla'.
split gv_text2 at space into table it_split.

loop at it_split into wa_split.

  read table it_operators into wa_operators with key value = wa_split-value.
  if sy-subrc eq 0.
     write: / gv_text2, 'contains operator', wa_operators-value.
  endif.

endloop.

Maybe there are other solutions, but you just have to try harder to find them.

Read only

ThomasZloch
Active Contributor
0 Likes
1,403

Try using the SYNTAX-CHECK command. I have constructed this from the ABAP online help:


DATA: itab TYPE TABLE OF string,
      mess TYPE string,
      lin  TYPE i,
      wrd  TYPE string,
      dir  TYPE trdir.

APPEND 'PROGRAM dummy.'               TO itab.
APPEND 'DATA a type c.'               TO itab.
APPEND 'DATA b type c.'               TO itab.
APPEND 'IF a GHE b.'                  TO itab.                "example, change as desired
APPEND 'ENDIF.'                       TO itab.

SYNTAX-CHECK FOR itab MESSAGE mess LINE lin WORD wrd
                 DIRECTORY ENTRY dir.

Check SY-SUBRC and the contents of "mess" to find out if your operator is supported.

Thomas

Read only

0 Likes
1,403

Nice one!