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

Need help related with loop

Former Member
0 Likes
509

Hi Experts,

I have written following code. The code is working fine. This code is creating records in table zhop_priority_pt. In the table the records are getting stored and priority of records is getting stored properly (zhop_priority_pt-z_prio). Every record of the table has distinct zhop_priority_pt-z_prio.

In the table zhop_priority_pt, I have stored location.

Now I have to calculate the priority (zhop_priority_pt-z_prio) seperately for each location.

For a given set of records in zhop_priority_pt, there may be say n distinct locations. So I have to create the priority list seperately for each location.

How I can do so? In my view, first of all I have to find out all distinct locations zhop_priority_pt-location.

After that for each location, I have to run the loop.

What changes should I make in the code.

IF NOT itab_prio[] IS INITIAL.
      CLEAR v_index.
      v_index = 1.
      SORT itab_prio BY z_prio_pts DESCENDING z_seniority descending z_hij_dob ascending. "


      LOOP AT itab_prio.

        MODIFY itab_prio TRANSPORTING z_prio_pts z_prio.
        if itab_prio-z_prio_pts > 0.
          itab_prio-z_prio = v_index.
          v_index = v_index + 1 .
          MODIFY itab_prio TRANSPORTING z_prio.
        endif.


      ENDLOOP.

      MODIFY zhop_priority_pt FROM TABLE itab_prio.
      COMMIT WORK.

    ENDIF.
  ENDIF.

Please help.

Regards,

Gary

1 ACCEPTED SOLUTION
Read only

deepak_dhamat
Active Contributor
0 Likes
477

hi gary ,

SORT itab_prio BY z_prio_pts DESCENDING z_seniority descending z_hij_dob ascending.

you are having

say itab having column location ,proirity

mundwa , 9

mundwa 10

mundwa 7

spec 6

spec 4

spec 1

sort itab by location descending proirity .

then you can also create another internal table say itab1 having only column location and prority

move distinct location to itab1 .

then sort both table according to requirment say

sort  itab by  location descending proirity  .
  sort itab1 by location  .

loop at  itab .

   at new location  .

    read table itab1 with key location  = itab-location  .

   if sy-subrc = 0 .


   move  priority to itab1 .

append   itab1 .
 endif.

endat .

endloop.

you can try this .

Regards

Deepak.

hi gary ,

SORT itab_prio BY z_prio_pts DESCENDING z_seniority descending z_hij_dob ascending.

you are having

say itab having column location ,proirity

mundwa , 9

mundwa 10

mundwa 7

spec 6

spec 4

spec 1

sort itab by location descending proirity .

then you can also create another internal table say itab1 having only column location and prority

move distinct location to itab1 .

then sort both table according to requirment say

sort  itab by  location descending proirity  .
  sort itab1 by location  .

loop at  itab .

   at new location  .

    read table itab1 with key location  = itab-location  .

   if sy-subrc = 0 .


   move  priority to itab1 .

append   itab1 .
 endif.

endat .

endloop.

you can try this .

Regards

Deepak.

3 REPLIES 3
Read only

deepak_dhamat
Active Contributor
0 Likes
478

hi gary ,

SORT itab_prio BY z_prio_pts DESCENDING z_seniority descending z_hij_dob ascending.

you are having

say itab having column location ,proirity

mundwa , 9

mundwa 10

mundwa 7

spec 6

spec 4

spec 1

sort itab by location descending proirity .

then you can also create another internal table say itab1 having only column location and prority

move distinct location to itab1 .

then sort both table according to requirment say

sort  itab by  location descending proirity  .
  sort itab1 by location  .

loop at  itab .

   at new location  .

    read table itab1 with key location  = itab-location  .

   if sy-subrc = 0 .


   move  priority to itab1 .

append   itab1 .
 endif.

endat .

endloop.

you can try this .

Regards

Deepak.

Read only

0 Likes
477

Hi Deepak,

Thanks. It is working fine after some minor modification.

Regards,

Gary

Modified Code

IF NOT itab_prio[] IS INITIAL.
      CLEAR v_index.
      v_index = 1.
      SORT itab_prio BY z_location z_prio_pts DESCENDING z_seniority descending z_hij_dob ascending.

      LOOP AT itab_prio.
        at new z_location.
          read table itab_prio with key
          z_location = itab_prio-z_location.

          if sy-subrc = 0.

            move itab_prio-z_location  to it_loc_pr.
            append it_loc_pr.
          endif.
        endat.
      endloop.

      SORT it_loc_pr BY location.
      DELETE ADJACENT DUPLICATES FROM it_loc_pr COMPARING location.
      loop at it_loc_pr.

        CLEAR v_index.
        v_index = 1.

        LOOP AT itab_prio where z_location = it_loc_pr-location.

        ENDLOOP.
      endloop.
      MODIFY zmyprogram_priority_pt FROM TABLE itab_prio.
      COMMIT WORK.
    ENDIF.

Read only

0 Likes
477

hi ,

Good work ... Congrats

This will boost you to and make you happy .

Regards

Deepak