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

Rearranging internal table data

Former Member
0 Likes
2,381

Hi Folks,

I need some help.

My internal table data is -

10381522KEY_NUMBER
10381522KEY_NUMBER
10381522KEY_NUMBER
10381522KEY_NUMBER
10381523MEDIUM
10381523MEDIUM
10381523MEDIUM
10381523MEDIUM
10381524CERTIFICATE_LANGUAG
10381524CERTIFICATE_LANGUAG
10381524CERTIFICATE_LANGUAG
10381524CERTIFICATE_LANGUAG
10381525PED
10381525PED
10381525PED
10381525PED

Now I want to change the order to

10381522KEY_NUMBER
10381523MEDIUM
10381524CERTIFICATE_LANGUAG
10381525PED
10381522KEY_NUMBER
10381523MEDIUM
10381524CERTIFICATE_LANGUAG
10381525PED
10381522KEY_NUMBER
10381523MEDIUM
10381524CERTIFICATE_LANGUAG
10381525PED
10381522KEY_NUMBER
10381523MEDIUM
10381524CERTIFICATE_LANGUAG
10381525PED

Pls let me know how can I achieve this ?

17 REPLIES 17
Read only

matt
Active Contributor
0 Likes
2,332

Add another field to your internal table of type integer. Where the text is KEY_NUMBER, give this field a value of 1. Where it is MEDIUM, give your field value 2, and so forth.

Then sort by your new field.

Read only

0 Likes
2,332

Hi,

If I am not mistaken there is such a field there.

But some how I have the feeling that there must be a field that is NOT in the internal and can be used for sorting (e.g. there are 4 occourances o "KEY_NUMBER"

with KEY_NUMBER in 1038152)

Regards.

Read only

anubhab
Active Participant
0 Likes
2,332

Hello Waseem,

    Just curious, do you need all the duplicate records?

Regards,

Anubhab

Read only

Former Member
0 Likes
2,332

I have another column where the relevant data is present so no duplication here.

Read only

Private_Member_15166
Active Contributor
0 Likes
2,332

If this is your only need then do like this.

1. sort internal table using middle field as i see it's already sorted.

2. delete adjacent duplicates from internal table, then you will get internal table with just 4 lines.

3. append this internal table to another table 4 times or to the same table 3 times.

if you have another need then post the actual question, rather than like this. People may be able to help you in better way with the original need.

regards

Dhananjay

Read only

0 Likes
2,332

No Dhananjay I have now mentioned my requirement clearly.

Read only

former_member206650
Active Participant
0 Likes
2,332

Hi,

since the second field is repeating ,take another internal table and fill it in order 2,3,4,5.

data :begin of my_table

field_2 type i ,

end of my_table.

data: wa type i.

wa = '2'.

append wa into my_table.

wa = '3'.

append wa into my_table.

wa = '4'.

append wa into my_table.

wa = '5'.

append wa into my_table.

loop at your_table into wa_your.

loop at my_table into wa_table.

read your_table into wa_final with key your_table-field_2 = wa_table-field_2.

append wa_final to lt_final. *this table contains ur sorted record.

endloop.

endloop.



hope it helps.

Read only

0 Likes
2,332

No the values are not fixed like 2,3,4,5 they will changing dynamically.

Read only

Former Member
0 Likes
2,332

Hi All,

Actually there is one more field in the table, the data is

A1SequenceA3A4
10381522KEY_NUMBERXyZ
10381522KEY_NUMBER123
10381522KEY_NUMBERqwjh
10381522KEY_NUMBERglmn
10381523MEDIUM 7893
10381523MEDIUM wedfd
10381523MEDIUM asdjch
10381523MEDIUM dvh
10381524CERTIFICATE_LANGUAG6498
10381524CERTIFICATE_LANGUAGasc
10381524CERTIFICATE_LANGUAGcip
10381524CERTIFICATE_LANGUAGasdf
10381525PED 6798
10381525PED quw
10381525PED 39785
10381525PED mxcnb

and the value under sequence is not fixed it is dynamic and can be from 1 to n.

So please let me know in such scenario how to achieve this.

Read only

former_member228804
Participant
0 Likes
2,332

Hello Waseem,

     Create a dummy internal table with the same type as your first internal table.

Now put a loop on the first internal table. IF wa_table1-second_column = 2. Append that work area to the dummy internal table.

Repeat the same procedure for second_column = 3, second_column = 4 and second_column = 5.

You will have the data in the dummy internal table now.

Regards,

Dnyanesh.

Read only

0 Likes
2,332

Hi Dnyanesh,

The sequence fields are not fixed!

Pls check my post once again

Read only

Former Member
0 Likes
2,332

Hi Was,

  1. Sort Your table by sequence or A3.
  2. Then delete the duplicate records comparing sequence or A3.
  3. Then describe the total table line.
  4. Then put the loop for the table, inside that loop use do statement with described line.
  5. Inside the do enddo. append the work area to internal table.

Hope it will meet your requirement .

Regards,

John.

Read only

0 Likes
2,332

Hi Was,

If you have any other unique column, Then pass all the sequence of data to dynamic internal table. then do like below,

loop at it_seq2 into wa_seq2.

lv_index = sy-tabix.

read table it_seq3 into wa_seq3 index lv_index.

read table it_seq4 as above

read table it_seq5 as above

append wa_final to it_final.

endloop.

Hope it will helpful to you.

Regards,

John.

Read only

former_member188282
Active Participant
0 Likes
2,331

Hi Waseem,

Try with below logic.


*&---------------------------------------------------------------------*

*& Report  ZSORT_DATA

*&

*&---------------------------------------------------------------------*

*&

*&

*&---------------------------------------------------------------------*

REPORT  zsort_data.

TYPES: BEGIN OF ly_data,

       fld3(25),

       fld1(10),

       fld2(1),

       fld4(10),

       END OF ly_data.

DATA: lt_data TYPE STANDARD TABLE OF ly_data.

DATA: ls_data TYPE ly_data.

ls_data-fld1 = '1038152'.

ls_data-fld2 = '2'.

ls_data-fld3 = 'KEY_NUMBER'.

APPEND ls_data TO lt_data.

ls_data-fld1 = '1038152'.

ls_data-fld2 = '2'.

ls_data-fld3 = 'KEY_NUMBER'.

APPEND ls_data TO lt_data.

ls_data-fld1 = '1038152'.

ls_data-fld2 = '2'.

ls_data-fld3 = 'KEY_NUMBER'.

APPEND ls_data TO lt_data.

ls_data-fld1 = '1038152'.

ls_data-fld2 = '2'.

ls_data-fld3 = 'KEY_NUMBER'.

APPEND ls_data TO lt_data.

ls_data-fld1 = '1038152'.

ls_data-fld2 = '3'.

ls_data-fld3 = 'MEDIUM'.

APPEND ls_data TO lt_data.

ls_data-fld1 = '1038152'.

ls_data-fld2 = '3'.

ls_data-fld3 = 'MEDIUM'.

APPEND ls_data TO lt_data.

ls_data-fld1 = '1038152'.

ls_data-fld2 = '3'.

ls_data-fld3 = 'MEDIUM'.

APPEND ls_data TO lt_data.

ls_data-fld1 = '1038152'.

ls_data-fld2 = '3'.

ls_data-fld3 = 'MEDIUM'.

APPEND ls_data TO lt_data.

ls_data-fld1 = '1038152'.

ls_data-fld2 = '4'.

ls_data-fld3 = 'CERTIFICATE_LANGUAG'.

APPEND ls_data TO lt_data.

ls_data-fld1 = '1038152'.

ls_data-fld2 = '4'.

ls_data-fld3 = 'CERTIFICATE_LANGUAG'.

APPEND ls_data TO lt_data.

ls_data-fld1 = '1038152'.

ls_data-fld2 = '4'.

ls_data-fld3 = 'CERTIFICATE_LANGUAG'.

APPEND ls_data TO lt_data.

ls_data-fld1 = '1038152'.

ls_data-fld2 = '4'.

ls_data-fld3 = 'CERTIFICATE_LANGUAG'.

APPEND ls_data TO lt_data.

DATA: lt_data_temp TYPE STANDARD TABLE OF ly_data.

DATA: ls_data_temp TYPE ly_data.

DATA: ls_data_temp1 TYPE ly_data.

lt_data_temp = lt_data.

LOOP AT lt_data INTO ls_data..

   WRITE:/ ls_data-fld3, ls_data-fld1, ls_data-fld2.

ENDLOOP.

WRITE:/ 'AFter Changing' COLOR 3.

LOOP AT lt_data INTO ls_data.

   LOOP AT lt_data_temp INTO ls_data_temp.

     ls_data_temp1 = ls_data_temp.

     AT NEW fld3.

       ls_data-fld4 = 'X'.

       MODIFY lt_data  FROM ls_data INDEX sy-tabix TRANSPORTING fld4.

       CLEAR: ls_data_temp.

       ls_data_temp = ls_data_temp1.

       WRITE:/ ls_data_temp-fld3, ls_data_temp-fld1,ls_data_temp-fld2.

     ENDAT.

   ENDLOOP.

   DELETE lt_data WHERE fld4 = 'X'.

   lt_data_temp = lt_data.

ENDLOOP.

Or You can use the below logic



*&---------------------------------------------------------------------*

*& Report  ZSORT_DATA

*&

*&---------------------------------------------------------------------*

*&

*&

*&---------------------------------------------------------------------*

REPORT  zsort_data.

TYPES: BEGIN OF ly_data,

       fld3(25),

       fld1(10),

       fld2(1),

       fld4(10),

       END OF ly_data.

DATA: lt_data TYPE STANDARD TABLE OF ly_data.

DATA: ls_data TYPE ly_data.

ls_data-fld1 = '1038152'.

ls_data-fld2 = '2'.

ls_data-fld3 = 'KEY_NUMBER'.

APPEND ls_data TO lt_data.

ls_data-fld1 = '1038152'.

ls_data-fld2 = '2'.

ls_data-fld3 = 'KEY_NUMBER'.

APPEND ls_data TO lt_data.

ls_data-fld1 = '1038152'.

ls_data-fld2 = '2'.

ls_data-fld3 = 'KEY_NUMBER'.

APPEND ls_data TO lt_data.

ls_data-fld1 = '1038152'.

ls_data-fld2 = '2'.

ls_data-fld3 = 'KEY_NUMBER'.

APPEND ls_data TO lt_data.

ls_data-fld1 = '1038152'.

ls_data-fld2 = '3'.

ls_data-fld3 = 'MEDIUM'.

APPEND ls_data TO lt_data.

ls_data-fld1 = '1038152'.

ls_data-fld2 = '3'.

ls_data-fld3 = 'MEDIUM'.

APPEND ls_data TO lt_data.

ls_data-fld1 = '1038152'.

ls_data-fld2 = '3'.

ls_data-fld3 = 'MEDIUM'.

APPEND ls_data TO lt_data.

ls_data-fld1 = '1038152'.

ls_data-fld2 = '3'.

ls_data-fld3 = 'MEDIUM'.

APPEND ls_data TO lt_data.

ls_data-fld1 = '1038152'.

ls_data-fld2 = '4'.

ls_data-fld3 = 'CERTIFICATE_LANGUAG'.

APPEND ls_data TO lt_data.

ls_data-fld1 = '1038152'.

ls_data-fld2 = '4'.

ls_data-fld3 = 'CERTIFICATE_LANGUAG'.

APPEND ls_data TO lt_data.

ls_data-fld1 = '1038152'.

ls_data-fld2 = '4'.

ls_data-fld3 = 'CERTIFICATE_LANGUAG'.

APPEND ls_data TO lt_data.

ls_data-fld1 = '1038152'.

ls_data-fld2 = '4'.

ls_data-fld3 = 'CERTIFICATE_LANGUAG'.

APPEND ls_data TO lt_data.

DATA: lt_data_temp TYPE STANDARD TABLE OF ly_data.

DATA: ls_data_temp TYPE ly_data.

DATA: ls_data_temp1 TYPE ly_data.

lt_data_temp = lt_data.

LOOP AT lt_data INTO ls_data..

   WRITE:/ ls_data-fld3, ls_data-fld1, ls_data-fld2.

ENDLOOP.

WRITE:/ 'AFter Changing' COLOR 3.

DATA: i TYPE i.

LOOP AT lt_data INTO ls_data.

   i = i + 1.

   ls_data-fld4 = i.

   MODIFY lt_data FROM ls_data INDEX sy-tabix.

   AT END OF fld1.

     CLEAR i.

   ENDAT.

ENDLOOP.

SORT lt_data BY fld4.

LOOP AT lt_data INTO ls_data..

   WRITE:/ ls_data-fld3, ls_data-fld1, ls_data-fld2.

ENDLOOP.

Check the attached output screen shot.

Regards,

Rajesh

Read only

0 Likes
2,331

Perfect its working!

Thanks Rajesh.

Read only

0 Likes
2,331

About the second method, relative order of lines is not well defined when you are sorting only on basis of fld4. Use SORT STABLE instead.

Read only

0 Likes
2,331

Hi Waseem,

Thanks for the confirmation.

Please close the ticket by marking as Correct/Helpful/.... .

Regards,

Rajesh