Application Development 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: 

records in internal table

Former Member
0 Kudos

I have an internal table with fields say Field1, Field2, Field3 ....

Now say this internal table contains 10 records

Say field1 = 'A' for first record

field1 = 'B' for second record

field1 = 'B' for third record

....

Now I want to display field1 = 'A' occurs 1 times

field1 = 'B' occurs 2 times

What should be the logic for the above, can I use AT-END AT or use sy-tabix.

Edited by: M N on Oct 15, 2008 10:21 AM

1 ACCEPTED SOLUTION

bpawanchand
Active Contributor
0 Kudos

Hi

Sort the itab first then you follow as below.

LOOP AT itab INTO wa.

 ADD 1 to w_count.
AT ENDOF <field>.
  WRITE :
      / wa-field , w_count, 'TIMES'.
   CLEAR w_count.
ENDAT.

ENDLOOP>

Regards

Pavan

5 REPLIES 5

Former Member
0 Kudos

Hi,

Before displaying the internal table,

1. sort it by field1.

2. loop at internal table - DO NOT use where condition here for the at new

or at end will not work.

3. AT NEW or AT END - display the field1.

Best Regards.

matt
Active Contributor
0 Kudos

>

> Hi,

> Before displaying the internal table,

> 1. sort it by field1.

> 2. loop at internal table - DO NOT use where condition here for the at new

> or at end will not work.

> 3. AT NEW or AT END - display the field1.

>

> Best Regards.

A good solution, but just to clarify.

Before point 1, set up a counter.

In point 2, add 1 to the counter

In point 3, write the counter after the field, and reset the counter.

matt

matt
Active Contributor
0 Kudos

Create another internal table count_itab with two fields - one the same as field1, the other as count. Loop through your other internal table itab1, like this:

DATA: count_wa LIKE LINE OF count_itab.

count_wa-count = 1.

LOOP AT itab1 INTO wa1.
  count_wa-field1 = itab1-field1.
  COLLECT count_wa INTO count_itab.
ENDLOOP.

You could do similar by adding a count field to your itab1, if you prefer.

matt

bpawanchand
Active Contributor
0 Kudos

Hi

Sort the itab first then you follow as below.

LOOP AT itab INTO wa.

 ADD 1 to w_count.
AT ENDOF <field>.
  WRITE :
      / wa-field , w_count, 'TIMES'.
   CLEAR w_count.
ENDAT.

ENDLOOP>

Regards

Pavan

Former Member
0 Kudos

Hi try this code i think it may help you.

<CODE>

DATA :BEGIN OF T_ITAB OCCURS 0,

F1,

END OF T_ITAB,

I TYPE I VALUE 0.

T_ITAB-F1 = 'A'.

APPEND T_ITAB.

T_ITAB-F1 = 'B'.

APPEND T_ITAB.

T_ITAB-F1 = 'A'.

APPEND T_ITAB.

T_ITAB-F1 = 'B'.

APPEND T_ITAB.

T_ITAB-F1 = 'A'.

APPEND T_ITAB.

T_ITAB-F1 = 'A'.

APPEND T_ITAB.

T_ITAB-F1 = 'c'.

APPEND T_ITAB.

SORT T_ITAB[].

LOOP AT T_ITAB.

I = I + 1.

AT END OF F1.

WRITE 😕 T_ITAB-F1,'occurs',I,'times'.

I = 0.

ENDAT.

ENDLOOP.

<CODE>

Regards.