Use ABAP to convert the rows and columns of an internal table

Source: Internet
Author: User

If you are interested, you can study how to convert the rows and columns of the internal table.

**************************************** *************************
* Author: chunbo. Xu
* Version: 1.0
* Create Date: 2005/08/02
* If you have any problems, contact me:
* MSN: xuchunbo0901@hotmail.com
* Email: xuchunbo0901@hotmail.com
* If you send it to another field, please write where it created
* This program converts the rows and columns of the internal table.
**************************************** **************************
Report z_matrix.
* ** There is nothing to say about the macro that adds data.
Define add_data.
RTAB1-NUM = & 1.
RTAB1-0 = & 2.
RTAB1-1 = & 3.
** If the table to be transposed has two columns, remove the column.
RTAB1-2 = & 4.
Append rtab1 to itab1.
End-of-definition.
* ** The column is defined as a number because it is a transpose of a simulated matrix.
* ** The inner table to be transposed
Data: Begin of rtab1,
Num (4) type C,
0 (4) type C,
1 (4) type C,
* ******* If the two columns are removed
2 (4) type C,
End of rtab1.
** This is the structure of the transposed table. You can find 10 columns and modify them by yourself.
Data: Begin of rtab2,
Num (4) type C,
0 (4) type C,
1 (4) type C,
2 (4) type C,
3 (4) type C,
4 (4) type C,
5 (4) type C,
6 (4) type C,
7 (4) type C,
8 (4) type C,
9 (4) type C,
End of rtab2.
** Define the table to be transposed as a sort table
Data: itab1 like sorted table of rtab1 with unique key num 0.
Data: itab2 like standard table of rtab2.
Data: itab1_temp like rtab1 occurs 0 with header line.
* ** Variable definition
Data: l_index type I, "Table Index
Rochelle temp (18) type C, "table element name
L_x type I, "Number of table rows
Rochelle H (2) Type C, "table rows
L_v (2) Type C, "table columns
Rochelle cy (2) Type C. "partition type of the column in the table
* ** Here is the number of columns in the table.
Constants: l_coun type I value 3,
* ********** This is the number of columns in the table minus 1.
Rochelle num type I value 2.
* ** This is the table element pointer.
Field-Symbols: type any,
Type any.
* ** This is a pointer to the table structure.
Field-Symbols: Like rtab1,
Like rtab2.
* ** If there are two columns, use the data annotated below to play
* Add_data '000000' 'aa' 1. 1 '.
* Add_data '000000' 'bb ''' 2. 1 '.
* Add_data '000000' 'cc''' 3. 1 '.
* Add_data '000000' 'dd''' 4. 1 '.
* Add_data '000000' 'A' 1. 2 '.
* Add_data '000000' 'bb ''' 2. 2 '.
* Add_data '000000' 'cc''' 3. 2 '.
* Add_data '000000' 'dd''' 4. 2 '.
* Add_data '000000' 'A' 1. 3 '.
* Add_data '000000' 'bb ''' 2. 3 '.
* Add_data '000000' 'cc''' 3. 3 '.
* Add_data '000000' 'dd''' 4. 3 '.
* Add_data '000000' 'ee ''5. 3 '.
* Add_data '000000' 'ff''' 6. 3 '.
* Add_data '000000' 'gg ''' 3000 '.
* Add_data '000000' 'A' 1. 4 '.
* Add_data '000000' 'bb ''' 2. 4 '.
* Add_data '000000' 'cc''' 3. 4 '.
* Add_data '000000' 'dd''' 4. 4 '.
* Add_data '000000' 'ee ''5. 4 '.
* Add_data '000000' 'aa' 1. 5 '.
* Add_data '000000' 'bb ''' 2. 5 '.
* Add_data '000000' 'cc''' 3. 5 '.
* Add_data '000000' 'dd''' 4. 5 '.
* Add_data '000000' 'ee ''5. 5 '.
* Add_data '000000' 'ff ''6. 5 '.
* Add_data '000000' 'gg ''' 5000 '.
* Add_data '000000' 'hh '8. 5 '.
* Add_data '000000' II ''' 9. 5 '.
* ** Use this data for three columns.
Add_data '000000' 'A' 11. 1' 12. 1 '.
Add_data '000000' 'bb ''11. 2' '12. 2 '.
Add_data '000000' 'cc' '11. 3' '12. 3 '.
Add_data '000000' 'dd''' 11. 4''' 12. 4 '.
Add_data '000000' 'A' 21. 1' 22. 1 '.
Add_data '000000' 'bb ''' 21. 2' 22. 2 '.
Add_data '000000' 'cc''' 21. 3' '22. 3 '.
Add_data '000000' 'dd''' 21. 4''' 22. 4 '.
Add_data '000000' 'A' 31. 1 '32. 1 '.
Add_data '000000' 'bb ''31. 2' '32. 2 '.
Add_data '000000' 'cc''' 31. 3' '32. 3 '.
Add_data '000000' 'dd''' 31. 4''' 32. 4 '.
Add_data '000000' 'ee ''31. 5' '32. 5 '.
Add_data '000000' 'ff ''31. 6 ''32. 6 '.
Add_data '000000' 'A' 41. 1' 42. 1 '.
Add_data '000000' 'bb ''41. 2'' 42. 2 '.
Add_data '000000' 'cc''' 41. 3 ''42. 3 '.
Add_data '000000' 'dd''' 41. 4''' 42. 4 '.
Add_data '000000' 'ee ''41. 5' 42. 5 '.
Add_data '000000' 'A' 51. 1' 52. 1 '.
Add_data '000000' 'bb ''51. 2' 52. 2 '.
Add_data '000000' 'cc''' 51. 3' '52. 3 '.
Add_data '000000' 'dd''' 51. 4''' 52. 4 '.
Add_data '000000' 'ee ''51. 5' 52. 5 '.
Add_data '000000' 'ff ''51. 6'' 52. 6 '.
Add_data '000000' 'gg ''51. 7'' 52. 7 '.
Add_data '000000' HH ''51. 8'' 52. 8 '.
* ** It is actually initializing the target table.
Loop at itab1 into rtab1.
At end of num.
* *** Add an empty data record to the first row. Add a few different num values.
RTAB2-NUM = ''.
Append rtab2 to itab2.
* *** Add several identical num values.
Do l_num times.
RTAB2-NUM = RTAB1-NUM.
Append rtab2 to itab2.
Enddo.
Endat.
Endloop.
* ** Initialize the number of Columns
L_h = '0 '.
L_v = '1 '.
Rochelle Cy = '0 '.
*** Processing started
Loop at itab1 into rtab1.
Append rtab1 to itab1_temp.
At end of num.
* *** Add an empty data record to the first row. Add a few different num values.
RTAB2-NUM = ''.
Append rtab2 to itab2.
* *** Add several identical num values.
Do l_num times.
RTAB2-NUM = RTAB1-NUM.
Append rtab2 to itab2.
Enddo.
Endat.
Endloop.
* ** Initialize the number of Columns
L_h = '0 '.
L_v = '1 '.
Rochelle Cy = '0 '.
*** Processing started
Loop at itab1 into rtab1.
Append rtab1 to itab1_temp.
At end of num.
* *** The same num is processed as a group.
Describe table itab1_temp lines l_x.
Do l_coun times.
L_index = l_index + 1.
* ***** Prepare the modified table data
Read Table itab2 assigning index l_index.
Do l_x times.
* ******* Get the element to be modified
Concatenate '-'l_h into l_temp.
Assign (l_temp).
Read Table itab1_temp assigning index l_v.
Concatenate '-' l_cy into l_temp.
Assign (l_temp).
* ******* Modification complete
=.
* ******* Add rows and columns
L_h = l_h + 1.
L_v = l_v + 1.
Enddo.
* ***** Add the column, initialize it, and continue processing the next column
L_cy = l_cy + 1.
L_h = '0 '.
L_v = '1 '.
Enddo.
* *** Remember to clear the temporary table
Clear: itab1_temp [], itab1_temp.
Endat.
** Reset the column name
Rochelle Cy = '0 '.
Endloop.
* ** The above is transposed. Sort it out below.
** Obtain the first row of data
Read Table itab2 assigning Index 1.
Loop at itab2 into rtab2.
* ** If there is more data than the first row below, give more data to the first row.
If SY-TABIX <> 1 and RTAB2-NUM = ''.
If> = rtab2.
Delete itab2 index SY-TABIX.
Else.
= Rtab2.
Delete itab2 index SY-TABIX.
Endif.
Endif.
Endloop.
*** Print it out and check it out.
Loop at itab2 into rtab2.
Write:/RTAB2-NUM, RTAB2-0, RTAB2-1, RTAB2-2, RTAB2-3, RTAB2-4,
RTAB2-6, RTAB2-7, RTAB2-8, RTAB2-9.
Endloop.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.