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

internal table

Former Member
0 Likes
593

hi friends,

can some one plz suggest me idea how to get the below O/P

i have 5 records in my itab

index matnr price matgrp count

1 100 100 AA 2

2 101 150 AA 1

3 103 100 BB 3

4 104 200 cc 4

5 105 300 DD 2

based on my count i should add the values of price

eg: when count is 2 i should add current plus next 2 records value

when it comes to last record i should add first 2 records value

here when it comes to 5th record it should add values of 1st & 2nd

record value

output should be like this

index matnr price matgrp count agg sale

1 100 100 AA 2 350

2 101 150 AA 1 250

3 103 100 BB 3 700

4 104 200 cc 4 850

5 105 300 DD 2 550

regards

soorya

6 REPLIES 6
Read only

Former Member
0 Likes
571

Hi, soorya.

1 100 100 AA 2 350

2 101 150 AA 1 250

3 103 100 BB 3 700

4 104 200 cc 4 850

5 105 300 DD 2 550

Is this right ? I think sale is 350 450 600 600 550. Am I right? And below is my code. Please reply me if wrong.

TYPES: BEGIN OF typ_itab,

index TYPE i,

matnr TYPE i,

price TYPE i,

matgrp(2) TYPE c,

count TYPE i,

sale TYPE i,

END OF typ_itab.

DATA: itab TYPE STANDARD TABLE OF typ_itab,

wa_itab TYPE typ_itab,

wa_itab_2 TYPE typ_itab.

DATA: price_1 TYPE i,

price_2 TYPE i.

DATA: lines TYPE i.

wa_itab-index = 1.

wa_itab-matnr = 100.

wa_itab-price = 100.

wa_itab-matgrp = 'AA'.

wa_itab-count = 2.

APPEND wa_itab TO itab.

wa_itab-index = 2.

wa_itab-matnr = 101.

wa_itab-price = 150.

wa_itab-matgrp = 'AA'.

wa_itab-count = 1.

APPEND wa_itab TO itab.

wa_itab-index = 3.

wa_itab-matnr = 103.

wa_itab-price = 100.

wa_itab-matgrp = 'BB'.

wa_itab-count = 3.

APPEND wa_itab TO itab.

wa_itab-index = 4.

wa_itab-matnr = 104.

wa_itab-price = 200.

wa_itab-matgrp = 'CC'.

wa_itab-count = 4.

APPEND wa_itab TO itab.

wa_itab-index = 5.

wa_itab-matnr = 105.

wa_itab-price = 300.

wa_itab-matgrp = 'DD'.

wa_itab-count = 2.

APPEND wa_itab TO itab.

LOOP AT itab INTO wa_itab.

WRITE:/ wa_itab-index ,

wa_itab-matnr,

wa_itab-price ,

wa_itab-matgrp,

wa_itab-count ,

wa_itab-sale.

ENDLOOP.

SKIP.

lines = LINES( itab ). "THE NUMBER OF ITAB RECORDS

DATA: total TYPE i.

LOOP AT itab INTO wa_itab.

total = ( wa_itab-index MOD lines ) + 1.

READ TABLE itab INTO wa_itab_2 WITH KEY INDEX = total.

price_1 = wa_itab_2-price.

total = ( ( wa_itab-index + 1 ) MOD lines ) + 1.

READ TABLE itab INTO wa_itab_2 WITH KEY INDEX = total.

price_2 = wa_itab_2-price.

wa_itab-sale = price_1 + price_2 + wa_itab-price.

MODIFY itab FROM wa_itab.

ENDLOOP.

LOOP AT itab INTO wa_itab.

WRITE:/ wa_itab-index ,

wa_itab-matnr,

wa_itab-price ,

wa_itab-matgrp,

wa_itab-count ,

wa_itab-sale.

ENDLOOP.

Result:

1 100 100 AA 2 0

2 101 150 AA 1 0

3 103 100 BB 3 0

4 104 200 CC 4 0

5 105 300 DD 2 0

1 100 100 AA 2 350

2 101 150 AA 1 450

3 103 100 BB 3 600

4 104 200 CC 4 600

5 105 300 DD 2 550

Read only

0 Likes
571

Why not to use a DO into the LOOP? Something like:

LOOP AT itab1 INTO wa1.
  l_index = sy-tabix.
  l_doindex = sy-tabix.
  DO wa1-count TIMES.
    ADD 1 TO l_doindex.
    READ TABLE itab1 INTO wa2 INDEX l_doindex.
    CHECK sy-subrc = 0.
    ADD wa2-amount TO wa1-amount.
  ENDDO.
  MODIFY itab1 FORM wa1 INDEX l_index.
ENDLOOP.

(this code is provided "as is" without any kind of warranty, and has NOT been tested. It's YOUR duty to make it work, because this piece of code is just a hint)

Read only

0 Likes
571

Thank you , Vicenç Lozano ! I'm a beginner , I don't know the system field: sy-tabix, I think it's very useful.

And I found that I misunderstood the soorya's purpose, because there is a condition: 'when count is X'.

Read only

0 Likes
571

Thanks you Vicenç Lozano , and I use your idea , update my code. And I think this time it's right. As below:

TYPES: BEGIN OF typ_itab,

index TYPE i,

matnr TYPE i,

price TYPE i,

matgrp(2) TYPE c,

count TYPE i,

sale TYPE i,

END OF typ_itab.

DATA: itab TYPE STANDARD TABLE OF typ_itab,

wa_itab TYPE typ_itab,

wa_itab_2 TYPE typ_itab.

DATA: lines TYPE i.

wa_itab-index = 1.

wa_itab-matnr = 100.

wa_itab-price = 100.

wa_itab-matgrp = 'AA'.

wa_itab-count = 2.

APPEND wa_itab TO itab.

wa_itab-index = 2.

wa_itab-matnr = 101.

wa_itab-price = 150.

wa_itab-matgrp = 'AA'.

wa_itab-count = 1.

APPEND wa_itab TO itab.

wa_itab-index = 3.

wa_itab-matnr = 103.

wa_itab-price = 100.

wa_itab-matgrp = 'BB'.

wa_itab-count = 3.

APPEND wa_itab TO itab.

wa_itab-index = 4.

wa_itab-matnr = 104.

wa_itab-price = 200.

wa_itab-matgrp = 'CC'.

wa_itab-count = 4.

APPEND wa_itab TO itab.

wa_itab-index = 5.

wa_itab-matnr = 105.

wa_itab-price = 300.

wa_itab-matgrp = 'DD'.

wa_itab-count = 2.

APPEND wa_itab TO itab.

LOOP AT itab INTO wa_itab.

WRITE:/ wa_itab-index ,

wa_itab-matnr,

wa_itab-price ,

wa_itab-matgrp,

wa_itab-count ,

wa_itab-sale.

ENDLOOP.

SKIP.

lines = LINES( itab ). "THE NUMBER OF ITAB RECORDS

DATA: l_index TYPE sy-tabix,

l_doindex TYPE sy-tabix.

LOOP AT itab INTO wa_itab.

wa_itab-sale = wa_itab-price.

l_index = sy-tabix.

l_doindex = sy-tabix.

DO wa_itab-count TIMES.

l_doindex = ( l_doindex MOD lines ) + 1.

READ TABLE itab INTO wa_itab_2 INDEX l_doindex.

CHECK sy-subrc = 0.

wa_itab-sale = wa_itab-sale + wa_itab_2-price.

ENDDO.

MODIFY itab FROM wa_itab INDEX l_index.

ENDLOOP.

LOOP AT itab INTO wa_itab.

WRITE:/ wa_itab-index ,

wa_itab-matnr,

wa_itab-price ,

wa_itab-matgrp,

wa_itab-count ,

wa_itab-sale.

ENDLOOP.

Result:

1 100 100 AA 2 0

2 101 150 AA 1 0

3 103 100 BB 3 0

4 104 200 CC 4 0

5 105 300 DD 2 0

1 100 100 AA 2 350

2 101 150 AA 1 250

3 103 100 BB 3 700

4 104 200 CC 4 850

5 105 300 DD 2 550

Read only

hymavathi_oruganti
Active Contributor
0 Likes
571

describe table itab lines lin.

loop at itab.

itab-agg_sale = itab-price.

if sy-tabix <> lin.

do itab-count times.

var = sy-index + 1.

read table itab index var.

itab-agg_sale = agg_sale + itab-price.

enddo.

elseif sy-index = lin.

read table itab index 1.

itab-agg_sale = itab-agg_sale + itab-price.

read table itab index 2.

itab-agg_sale = itab-agg_sale + itab-price.

endif.

modify itab transporting agg_sale.

clear itab.

endloop.

Message was edited by:

Hymavathi Oruganti

Read only

Former Member
0 Likes
571

Hi Soorya,

describe table itab lines no_of_lines.

clear wa1_itab.

loop at itab into wa1_itab.

if no_of_lines eq sy-tabix.

idx1 = 1.

idx2 = wa1_itab-count.

else.

idx1 = sy-tabix.

idx2 = idx1 + wa1_itab-count.

endif.

clear wa2_itab.

loop at itab into wa2_itab from idx1 to idx2.

wa1_itab-sale = wa1_itab-sale + wa2_itab-price.

endloop.

modify itab from wa1_itab transporting sale.

endloop.

Regards,

Preethi.

Message was edited by:

Preethi Gurramkonda