More on ABAP Internal Tables

DESCRIBE TABLE is the statement to get the attributes like number of lines, line width of each row etc. of the internal table. DESCRIBE TABLE statement also fills the system fields SY-TFILL (Current no. of lines in internal table), SY-TLENG (line width of internal table) etc.

DESCRIBE TABLE <internal table> [LINES <lines>].

SORT is the statement to sort an ABAP internal table. We can specify the direction of the sort using the additions ASCENDING and DESCENDING. The default is ascending.

SORT <internal table> [ASCENDING|DESCENDING]

We can also delete the adjacent duplicates from an internal table by using the following statement.

DELETE ADJACENT DUPLICATE ENTRIES FROM <internal table> 
                 [COMPARING <f1> <f2> ... |ALL FIELDS].

COMPARING ALL FIELDS is the default. If we do not specify the COMPARING addition, then the system compares all the fields of both the lines. If we specify fields in the COMPARING clause, then the system compares only the fields specified after COMPARING of both the lines. If at least one line is deleted, the system sets SY-SUBRC to 0, otherwise to 4.

*--------------------------------------------------------------*
*Data Types
*--------------------------------------------------------------*
TYPES: BEGIN OF ty_student,
       id(5)     TYPE n,
       name(10)  TYPE c,
       place(10) TYPE c,
       age       TYPE i,
       END OF ty_student.

*--------------------------------------------------------------*
*Data Declaration
*--------------------------------------------------------------*
DATA: gwa_student TYPE ty_student.
DATA: it  TYPE TABLE OF ty_student.
DATA: gv_lines TYPE i.

gwa_student-id     = 1.
gwa_student-name   = 'JOHN'.
gwa_student-place  = 'London'.
gwa_student-age    = 20.
APPEND gwa_student TO it.

gwa_student-id     = 2.
gwa_student-name   = 'JIM'.
gwa_student-place  = 'New York'.
gwa_student-age    = 21.
APPEND gwa_student TO it.

gwa_student-id     = 3.
gwa_student-name   = 'JACK'.
gwa_student-place  = 'Bangalore'.
gwa_student-age    = 20.
APPEND gwa_student TO it.

gwa_student-id     = 4.
gwa_student-name   = 'ROB'.
gwa_student-place  = 'Bangalore'.
gwa_student-age    = 22.
APPEND gwa_student TO it.

gwa_student-id     = 2.
gwa_student-name   = 'JIM'.
gwa_student-place  = 'New York'.
gwa_student-age    = 21.
APPEND gwa_student TO it.

DESCRIBE TABLE it LINES gv_lines.
WRITE:/ 'No. of lines in IT : ', gv_lines.

WRITE:/ 'SY-TFILL : ', sy-tfill.
WRITE:/ 'SY-TLENG : ', sy-tleng.

WRITE:/ 'Values in IT before SORT' COLOR 4.

WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5,
        37 'Age' COLOR 5.
LOOP AT it INTO gwa_student.
  WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place,
          gwa_student-age.
ENDLOOP.

WRITE:/ 'Values in IT after SORT' COLOR 4.

*SORT by name
SORT it BY name DESCENDING.

WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5,
        37 'Age' COLOR 5.
LOOP AT it INTO gwa_student.
  WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place,
          gwa_student-age.
ENDLOOP.

WRITE:/ 'Values in IT after deleting duplicates' COLOR 4.

*Delete duplicates
SORT it.
DELETE ADJACENT DUPLICATES FROM it.

WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5,
        37 'Age' COLOR 5.
LOOP AT it INTO gwa_student.
  WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place,
          gwa_student-age.
ENDLOOP.

WRITE:/ 'Values in IT after deleting duplicates comparing place' COLOR 4.

*Delete duplicates comparing only place
SORT it BY place.
DELETE ADJACENT DUPLICATES FROM it COMPARING place.

WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5,
        37 'Age' COLOR 5.
LOOP AT it INTO gwa_student.
  WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place,
          gwa_student-age.
ENDLOOP.

Output

more-it-1

We can exit out of LOOP/ENDLOOP processing using EXIT, CONTINUE and CHECK similar to all other LOOPS.

We can  also initialize the internal table using FREE, CLEAR and REFRESH statements. CLEAR and REFRESH just initializes the internal table where as FREE initializes the internal table and releases the memory space.

*--------------------------------------------------------------*
*Data Types
*--------------------------------------------------------------*
TYPES: BEGIN OF ty_student,
       id(5)     TYPE n,
       name(10)  TYPE c,
       place(10) TYPE c,
       age       TYPE i,
       END OF ty_student.

*--------------------------------------------------------------*
*Data Declaration
*--------------------------------------------------------------*
DATA: gwa_student TYPE ty_student.
DATA: it  TYPE TABLE OF ty_student.
DATA: gv_lines TYPE i.

gwa_student-id     = 1.
gwa_student-name   = 'JOHN'.
gwa_student-place  = 'London'.
gwa_student-age    = 20.
APPEND gwa_student TO it.

gwa_student-id     = 2.
gwa_student-name   = 'JIM'.
gwa_student-place  = 'New York'.
gwa_student-age    = 21.
APPEND gwa_student TO it.

WRITE:/ 'Values in IT before initializing' COLOR 4.

WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5,
        37 'Age' COLOR 5.
LOOP AT it INTO gwa_student.
  WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place,
          gwa_student-age.
ENDLOOP.

*Initialize IT
CLEAR it.

SKIP.
WRITE:/ 'Values in IT before initializing' COLOR 4.
WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5,
        37 'Age' COLOR 5.
LOOP AT it INTO gwa_student.
  WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place,
          gwa_student-age.
ENDLOOP.
*If no records are processed inside LOOP, then SY-SUBRC <> 0
IF sy-subrc <> 0.
  WRITE:/ 'No records found.'.
ENDIF.

SKIP.
*We can also use IS INITIAL to check any records found in IT
IF it IS INITIAL.
  WRITE:/ 'No records found in IT.'.
ENDIF.

Output

more-it-2


4 Comments

  1. As you said free releases the memory space
    So my que is after freeing the internal table when giving new data then also the output is coming so how come it’s releasing the memory?

Comments are closed.