November 10, 2015

Clear, refresh and free in ABAP

In contrast to other languages where we need to make use of its basic value to initialize the variables, in SAP we don't have to make such thing. Besides initialize or clear a variable with its basic value like integers to zero and strings to space(s), in SAP we can make use of the command 'CLEAR' that can also be used with any variable and structure. For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
DATA: lv_number TYPE n, lv_char type c.
lv_number = 1.
WRITE lv_number.
CLEAR lv_number. " Initialize to 0
WRITE lv_number.

lv_char = 'A'.
WRITE lv_char.
CLEAR lv_char. " Initialize to space[s]
WRITE lv_char.

The same applies to work areas;

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
TYPES: BEGIN OF ty_data,
  matnr TYPE matnr,
  value(2) TYPE n,
  erdat TYPE erdat,
END OF ty_data.

DATA wa_data TYPE ty_data.
wa_data-matnr = '1'.
wa_data-value = '01'.
wa_data-erdat = sy-datum.

WRITE: 'Work area with data', wa_data.

CLEAR wa_data. " All members initialized

WRITE: / 'Work area cleared', wa_data.

The output is the following:

The effect on the content of field symbols is the same as work areas.

For internal tables we also have the option to use the CLEAR command and 2 options more: REFRESH and FREE. Let's show how they work with an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
TYPES: BEGIN OF ty_data,
  matnr TYPE matnr,
  value(5) TYPE n,
  erdat TYPE erdat,
END OF ty_data.

DATA: it_data TYPE STANDARD TABLE OF ty_data,
wa_data LIKE LINE OF it_data,
lv_count type i.

DO 10 TIMES.
  wa_data-matnr = sy-index.
  wa_data-value = '01'.
  wa_data-erdat = sy-datum.
  APPEND wa_data TO it_data.
ENDDO.

DESCRIBE TABLE it_data LINES lv_count. " Variable lv_count has the value 10

CLEAR it_data[]. " This will initialize all the content of table including the table header
REFRESH it_data. " This will initialize all the content of table but not the table header
FREE it_data. " This will initialize all the content of table including the table header and will release from memory the table

CLEAR <itab>[]: Will initialize all the content of table including the table header. Check out the brackets.

REFRESH <itab>: Will initialize all the content of table but not the table header. **

FREE <itab>: Will initialize all the content of table including the table header and will release the table from memory.

** What is the table header? Well, when a table is defined like this:

1
DATA it_data TYPE STANDARD TABLE OF ty_data WITH HEADER LINE.

It means that you can use a work area defined within the table so you don't need to create a work area for this. However, it is not advisable, it's better to use work areas and field symbols. So please, don't take this as a regular practice, just have in mind that a lot of legacy code still has this type of declaration.

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
DATA: it_data TYPE STANDARD TABLE OF ty_data WITH HEADER LINE,
      lv_count type i.

DO 10 TIMES.
  it_data-matnr = sy-index.
  it_data-value = '01'.
  it_data-erdat = sy-datum.
  APPEND it_data.
ENDDO.

REFRESH it_data. " The work area within IT_DATA still has data.

WRITE it_data. " This will output of the work area'               10 0000120151110'

Well, that's all for this now.

See you in the next.

Hope it helps.
Please Share it! :)

1 comment :