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

small query reg interernal table

Former Member
0 Likes
625

hi all..

i am new to this group. i want to ask small query regarding internal table.

i have 2 feilds in the iternal table. they r curr and amount.

data: begin of itab occurs 0,

cur(3) type c ,

amt type i,

end of itab.

data: begin of wa_itab,

cur(3) type c ,

amt type i,

end of wa_itab.

itab-cur = 'EUR'.

itab-amt = 111.

append itab.

itab-cur = 'USR'.

itab-amt = 222.

append itab.

itab-cur = 'EUR'.

itab-amt = 777.

append itab.

itab-cur = 'USR'.

itab-amt = 333.

append itab.

sort itab by cur.

loop at itab into wa_itab where cur = 'EUR'.

at first.

sum .

endat.

write:/ wa_itab-cur,wa_itab-amt.

*read table itab

endloop.

i want the totals for each currency. please correct my code. please help in making this out.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
591

Try out this:

data: begin of itab occurs 0,

cur(3) type c ,

amt type i,

end of itab.

<b>data: sum type i.

data: l_index type sy-tabix.</b>

data: begin of wa_itab,

cur(3) type c ,

amt type i,

end of wa_itab.

itab-cur = 'EUR'.

itab-amt = 111.

append itab.

itab-cur = 'USR'.

itab-amt = 222.

append itab.

itab-cur = 'EUR'.

itab-amt = 777.

append itab.

itab-cur = 'USR'.

itab-amt = 333.

append itab.

<b>clear sum.</b>

sort itab by cur.

<b>loop at itab into wa_itab.

l_index = sy-tabix.

sum = sum + wa_itab-amt.

at end of curr.

  • as in at end of statement all the non-numeric fields will be filled with characters u need to * read the table again.

read itab into wa_itab index l_index.

wa_itab-amt = sum.

write:/ wa_itab-cur,wa_itab-amt.

clear sum.

endat.</b>

endloop.

hi all..

i am new to this group. i want to ask small query regarding internal table.

i have 2 feilds in the iternal table. they r curr and amount.

data: begin of itab occurs 0,

cur(3) type c ,

amt type i,

end of itab.

data: begin of wa_itab,

cur(3) type c ,

amt type i,

end of wa_itab.

itab-cur = 'EUR'.

itab-amt = 111.

append itab.

itab-cur = 'USR'.

itab-amt = 222.

append itab.

itab-cur = 'EUR'.

itab-amt = 777.

append itab.

itab-cur = 'USR'.

itab-amt = 333.

append itab.

sort itab by cur.

loop at itab into wa_itab where cur = 'EUR'.

at first.

sum .

endat.

write:/ wa_itab-cur,wa_itab-amt.

*read table itab

endloop.

i want the totals for each currency. please correct my code. please help in making this out.

4 REPLIES 4
Read only

Former Member
0 Likes
591

Hi Arjun,

Use <b>COLLECT</b> statement instead of Append.

That should meet ur requirement.

Reward points if that helps.

Manish

Corrected Program:

itab-cur = 'EUR'.

itab-amt = 111.

collect itab.

itab-cur = 'USR'.

itab-amt = 222.

collect itab.

itab-cur = 'EUR'.

itab-amt = 777.

collect itab.

itab-cur = 'USR'.

itab-amt = 333.

collect itab.

sort itab by cur.

loop at itab into wa_itab." where cur = 'EUR'.

*

*at first.

*sum .

*endat.

write:/ wa_itab-cur,wa_itab-amt.

*read table itab

endloop.

Message was edited by: Manish Kumar

Read only

Former Member
0 Likes
591

use at new cur and not at first

loop at itab into wa_itab.

at new cur.

sum .

write:/ itab-cur,itab-amt.

endat.

endloop.

Read only

anversha_s
Active Contributor
0 Likes
591

Hi,

you can use the collect statement in this way.

Collect:

When the line is inserted, the system checks whether there is already a table entry that matches the key.

If there is no corresponding entry already in the table,

the COLLECT statement has the same effect as inserting the new line.

If an entry with the same key already exists,

the COLLECT statement does not append a new line,

but adds the contents of the numeric fields in the work area to the contents of the numeric fields in the existing entry.

You should only use the COLLECT statement if you want to create summarized tables.

If you use other statements to insert table entries, you may end up with duplicate entries.

GO THROUGH THIS LINK

http://www.sts.tu-harburg.de/teaching/sap_r3/ABAP4/collect.htm

1. Can i use a collect statement here

We can use collect statement in all internal tables.

2. The only MAIN CONCEPT is

3. that it will SUM up the Quantity fields,

based upon

the COMBINATION of all Character Fields.

change code like this.


data: begin of itab occurs 0,
cur(3) type c ,
amt type i,
end of itab.

data: begin of wa_itab,
cur(3) type c ,
amt type i,
end of wa_itab.

itab-cur = 'EUR'.
itab-amt = 111.
<b>collect itab.</b>

itab-cur = 'USR'.
itab-amt = 222.
collect itab.

itab-cur = 'EUR'.
itab-amt = 777.
collect itab.

itab-cur = 'USR'.
itab-amt = 333.
collect itab.


sort itab by cur.
loop at itab into wa_itab.
write:/ wa_itab-cur,wa_itab-amt.
*read table itab
endloop.

regards

Anver

if hlped pls mark points

Read only

Former Member
0 Likes
592

Try out this:

data: begin of itab occurs 0,

cur(3) type c ,

amt type i,

end of itab.

<b>data: sum type i.

data: l_index type sy-tabix.</b>

data: begin of wa_itab,

cur(3) type c ,

amt type i,

end of wa_itab.

itab-cur = 'EUR'.

itab-amt = 111.

append itab.

itab-cur = 'USR'.

itab-amt = 222.

append itab.

itab-cur = 'EUR'.

itab-amt = 777.

append itab.

itab-cur = 'USR'.

itab-amt = 333.

append itab.

<b>clear sum.</b>

sort itab by cur.

<b>loop at itab into wa_itab.

l_index = sy-tabix.

sum = sum + wa_itab-amt.

at end of curr.

  • as in at end of statement all the non-numeric fields will be filled with characters u need to * read the table again.

read itab into wa_itab index l_index.

wa_itab-amt = sum.

write:/ wa_itab-cur,wa_itab-amt.

clear sum.

endat.</b>

endloop.