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: 

How can i use "Collect" statement?

0 Kudos

Dears,

i have a table like that:

DATA: BEGIN OF it_display  OCCURS 0,


        zodb(20)     TYPE c,

        zmonth01     TYPE i,

        zmonth02     TYPE i,

        zmonth03     TYPE i,

        zmonth04     TYPE i,

        zmonth05     TYPE i,

    END OF it_display.

For each zodb field i found month01 values, after that for each zodb field i found month02 values ... and so on. Like that:

loop at it_odb. --> (zodb field values)

    DATAlv_pmonth(20TYPE c.

     zcount = 1.

    UNASSIGN  <fs_pmonth> .

    ztext = it_odb-cetxt.

.......

     CONCATENATE 'it_display-zmonth0' zcount

        INTO lv_pmonth .

     ASSIGN (lv_pmonth) TO <fs_pmonth> .

     it_display-zodb = ztext.

     <fs_pmonth> = g_count. -->( i assign it_display-zmonth01,2 values ... and so on.no problem)

     sort it_display BY zodb .

     collect it_display.--> problem accured!!!

endloop.


zcount = zcount + 1.


Data sample:


it_display for first step:


zodb  month01 month02 mont03 mont04 month05  -->it_display table fields

  A         1000

  B            0             this step no problem...

  C         383



it_display for second step:


zodb  month01 month02 mont03 mont04 month05  -->it_display table fields

A        1383        734

  B         383          0

  C         766          .......


As you see, previous values was changed..i want to collect data depends on zodb field.But my code collect month01,month02...etc.  also.


How can i fix that ?



Thank you



1 ACCEPTED SOLUTION

jrg_wulf
Active Contributor
0 Kudos

Oh dear, where to begin,

First -  only a minor issue in this context but nevertheless worth mentioning:

COLLECT will temporarily build a hashed table as long as you don't mess with the table content and only use collect. Meaning, the moment you use sort, you'll loose that advantage. For big tables, this can be an issue.

Second - you are using obsolete concepts like implicit workareas, named the same as your table. This does not increase the readability of your code.

Third: i can't see how you proceed to step 2, 3 ...but assumed you have an outer loop,

your increment zcount = zcount + 1 is rendered useless by zcount = 1 inside your loop.

Fourth: i may have missed it, but i don't see, where you clear your implicit workarea after reassigning your month-field. So this is probably the cause of your problem since it_display-zmonth01 will still have the last value of 383, which, what a coincidence, is exactly the value added to each line

Nevertheless, consider the other points.

Best regards - Jörg

5 REPLIES 5

jrg_wulf
Active Contributor
0 Kudos

Oh dear, where to begin,

First -  only a minor issue in this context but nevertheless worth mentioning:

COLLECT will temporarily build a hashed table as long as you don't mess with the table content and only use collect. Meaning, the moment you use sort, you'll loose that advantage. For big tables, this can be an issue.

Second - you are using obsolete concepts like implicit workareas, named the same as your table. This does not increase the readability of your code.

Third: i can't see how you proceed to step 2, 3 ...but assumed you have an outer loop,

your increment zcount = zcount + 1 is rendered useless by zcount = 1 inside your loop.

Fourth: i may have missed it, but i don't see, where you clear your implicit workarea after reassigning your month-field. So this is probably the cause of your problem since it_display-zmonth01 will still have the last value of 383, which, what a coincidence, is exactly the value added to each line

Nevertheless, consider the other points.

Best regards - Jörg

former_member201275
Active Contributor
0 Kudos

Your code is quite incorrect, but hopefully this will give you a start:

data: begin of display,

zodb(20),

zmonth01 type I,

zmonth02 type I,

zmonth03 type I,

zmonth04 type I,

zmonth05 type I,

end of display.

data: ls_display type display,

lt_display type hashed table of display with unique key zodb.

*    fill ls_display

·     *   remove your SORT statement

collect ls_display into lt_display.

I would recommend Googling "COLLECT statement abap tutorial" to understand the basics.

Former Member
0 Kudos

g_count values coming from where?

former_member206650
Active Participant
0 Kudos

Hi ÇAĞLA,

you have made the logic very complex and i think you are not getting as planned..collect will add the previous entry with the new one and get u summed solution.

vishnu



0 Kudos

Dear ,

Thank you very much for your answer.

i forgot to clear implicit workarea after reassingned that field.When i clear it,problem was solved.

Thank you