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

How to code this logic?

sunil_khemchand2
Participant
0 Likes
799

I have an internal table with two fields FLD1 & FLD2(flag). I have to make sure that for same FLD1, there has to be only one FLD2 flag:

    FLD1           FLD2
       A            X
       A            
       B            X
       B            X
       B

For example in the above example, 'B' has two 'X' therefore, I have to give an error message saying " Maintain only one 'X' value for B"

How do I do this?

Thanks

Sg

1 ACCEPTED SOLUTION
Read only

former_member194669
Active Contributor
0 Likes
779

Hi,


sort itab by fld1.

loop at itab.
  at end of fld1.
    move 'Y' to v_flg.
  endat.
  if itab-fld2 eq 'X'.
     v_no = v_no + 1.
  endif.
  if v_flg eq 'Y'.
  if v_no gt 1 .
     message e999(yyy) "<<<<<<<
  endif.
  clear: v_flg, V_NO.
  endif.
endloop.
  

aRs

8 REPLIES 8
Read only

former_member194669
Active Contributor
0 Likes
780

Hi,


sort itab by fld1.

loop at itab.
  at end of fld1.
    move 'Y' to v_flg.
  endat.
  if itab-fld2 eq 'X'.
     v_no = v_no + 1.
  endif.
  if v_flg eq 'Y'.
  if v_no gt 1 .
     message e999(yyy) "<<<<<<<
  endif.
  clear: v_flg, V_NO.
  endif.
endloop.
  

aRs

Read only

Former Member
0 Likes
779

sorry

Read only

dev_parbutteea
Active Contributor
0 Likes
779

HI

wa_itab and wa-previous are two work areas of same internal table.

loop at itab into wa_itab.

if sy-tabix = 1.

exit.

endif.

if wa_itab-FLD1 = wa_previous-FLD1

and wa_itab-FLD2 = wa_previous-FLD2

*display error message

endif.

clear wa_previous

wa_previous = wa_itab.

clear wa_itab.

endloop.

Read only

Former Member
0 Likes
779

hi,

try like this,

sort itab by fld1 asc.

data: flag type c value 0.

loop at itab.

write:/10 itab-fld1.

if flag <=1.

at new itab.

itab-fld2 = 'X'.

modify itab.

endat.

else

flag = flag + 1.

message e000(zaluri) with 'record' sy-tabix 'has more values'.

endif.

if useful reward some points.

with regards,

Suresh Aluri.

Read only

Former Member
0 Likes
779

data : count type i.

sort itab by fld1.

loop at itab.

at new fld1.

clear : count.

endat.

if itab-fld2 = 'X'.

count = count + 1.

endif.

if count gt 1.

message 'Maintain only one 'X' value' type 'E'.

endif.

endloop.

regards

shiba dutta

Read only

Former Member
0 Likes
779

try the code given below. It may help to code occording to ur desired logic.


DATA: BEGIN OF it  OCCURS 10,
      fld1 TYPE c,
      fld2 TYPE c,
      END OF it.

PARAMETERS: pfld1 type c .
            pfld2 type  c.
data temp type i.
start-of-SELECTION.
loop at it.
  if it-fld1 = pfld1 and it-fld2 = pfld2.
  temp = 1.
   STOP.
  endif.
   ENDLOOP.

end-of-SELECTION.
if temp = 0.
it-fld1 = pfld1. it-fld2 = pfld2.
append it.
write: / 'data append'.
sort it by fld1.
loop at it.
write: / it-fld1, it-fld2.
ENDLOOP.
else.
WRITE / 'Maintain only one',pfld2, 'value for',pfld1.
it-fld1 = pfld1. it-fld2 = ''.
APPEND it.
sort it by fld1.
loop at it.
write: / it-fld1, it-fld2.
ENDLOOP.
endif.

regards

Vijaykumar Reddy. S

Read only

Former Member
0 Likes
779

Hi sugnani,

All the above methods work really guuud,

but different logic. This code below is one more approach , try this also

delcare one more internal table same as first internal table .(say.) itab2.


data: w_temp(1) type c.
itab2[] = itab1[].
sort itab2 ascending by fld1 fld2.
delete adjacent duplicates from itab2 comparing fld1 fld2.
clear w_temp.

loop at itab2.
if ( itab2-fld1 = w_temp )
*display message.
endif.
w_temp = itab2-fld1.
endloop.

Hope you found it useful... ;)


Regards,

SJ

Read only

sunil_khemchand2
Participant
0 Likes
779

Thanks everyone for your response. I have assigned the points to everyone.