ABAP Keyword Documentation → ABAP − Reference → Creating Objects and Values → NEW - Instance Operator
NEW - Internal Tables
Other versions:
7.31 | 7.40 | 7.54
Syntax
... NEW dtype|#( [let_exp]
[BASE itab]
[FOR for_exp1
FOR for_exp2
... ]
( line_spec1 )
( line_spec2 )
... ) ...
Extras
1. ... BASE itab
2. ... FOR for_exp
Effect
If dtype
is a tabular data type or #
stands for a type like this, the table rows of the constructed table are created as follows:
- Firstly, an optional
LET
expression let_exp can be specified to define local auxiliary fields whose values can be used to construct the table rows.
- An optional start value for the content can be specified for the content of the internal table after
BASE
(see below).
- One more optional consecutive
iteration expressions can then be specified using
FOR
(see below).
- The table rows are then constructed in one or more sets of internal parentheses by specifying
line_spec
. Each set of inner parentheses constructs one or more rows in accordance with the information inline_spec
and inserts them into the new internal table in accordance with the rules for the statement INSERT ... INTO TABLE. The object is inserted in the order of the parentheses.
The constructed rows must meet the requirements of the statement
INSERT for inserting work areas using table keys and therefore be compatible with the row type. There is one exception to this: When constructing a
standard table, where
the rows are only appended, the value can be shorter than the row length (for row types c
and x
), in which case it is padded on the right with blanks or hexadecimal 0.
Notes
- When a constructor expression is assigned to a reference variable using
NEW
, the original reference is made available in the entire expression in the target variable. The target variable is not overwritten until the expression is closed. In the case of the value operatorVALUE
, however, the target variable can only be assigned to a helper variable usingLET
and is then no longer available.
- The operand type for
line_spec
in the inner parentheses is the row type of the table type specified usingdtype
or#
, which means it is always uniquely identifiable. Constructor expressions in this place can therefore always derive the required data type using#
.
Example
Constructs an anonymous internal table with a structured row type with substructures and tabular components.
TYPES: BEGIN OF t_struct1,
col1 TYPE i,
col2 TYPE i,
END OF t_struct1,
BEGIN OF t_struct2,
col1 TYPE i,
col2 TYPE t_struct1,
col3 TYPE TABLE OF t_struct1 WITH EMPTY KEY,
END OF t_struct2,
t_itab TYPE TABLE OF t_struct2 WITH EMPTY KEY.
DATA dref TYPE REF TO data.
dref = NEW t_itab( ( col1 = 1
col2-col1 = 1
col2-col2 = 2
col3 = VALUE #( ( col1 = 1 col2 = 2 )
( col1 = 3 col2 = 4 ) ) )
( col1 = 2
col2-col1 = 2
col2-col2 = 4
col3 = VALUE #( ( col1 = 2 col2 = 4 )
( col1 = 6 col2 = 8 ) ) ) ).
Examples
See also the examples for the value operator VALUE
.
Addition 1
... BASE itab
Effect
An addition, BASE
, followed by an internal table, itab
, can be specified in front of the lines that you want to insert. This is a
functional operand
position. The row type of itab
must be convertible to the row type of
the return value. If BASE
is specified, the content of itab
is assigned to the return value before the individual rows are inserted. If the character #
is specified for the type of the return value and the type cannot be determined from the operand position
of the constructor expression, the type of itab
is used for this expression (if identifiable).
Example
Constructs three anonymous internal tables with an elementary row type. The first table is filled with
three rows. The second row is initial. The second and third tables are filled with the rows of the first
table and three further rows. Using BASE
and LINES OF
here has the same effect.
TYPES t_itab TYPE TABLE OF i WITH EMPTY KEY.
DATA(dref1) = NEW t_itab( ( 1 ) ( ) ( 3 ) ).
DATA(dref2) = NEW t_itab( BASE dref1->*
( ) ( 5 ) ( ) ).
DATA(dref3) = NEW t_itab( ( LINES OF dref1->* )
( ) ( 5 ) ( ) ).
cl_demo_output=>write( dref1->* ).
cl_demo_output=>write( dref2->* ).
cl_demo_output=>display( dref3->* ).
Addition 2
... FOR for_exp
Effect
If one or more iteration
expressions are specified consecutively using FOR
,
this means that the rows constructed in
line_spec for every iteration of the last FOR
expression are inserted
into the target table. When constructing table rows in
line_spec
, the visible local work areas and field symbols of the iteration expressions can be used to construct table rows.
- If conditional iterations are used, the new table rows are created in freely defined iteration steps.
- If table iterations are used, the rows of existing internal tables are evaluated. These are known as table comprehensions, since in this case new table rows can be constructed from the rows of existing internal tables.
Notes
- The term "table comprehension" is derived from similar concepts used in many other languages, where it is also known as "list comprehension".
- Table filtering can be implemented more efficiently using the
filter operator
FILTER
.
- Conditional iterations and table comprehensions are also possible with
FOR
expressions for mesh paths.
Example
Constructs an anonymous internal table with an elementary row type and fills it with square numbers using an iteration expression.
TYPES t_itab TYPE TABLE OF i WITH EMPTY KEY.
DATA(dref) = NEW t_itab( FOR i = 1 UNTIL i > 10
( ipow( base = i exp = 2 ) ) ).
cl_demo_output=>display( dref->* ).
Executable Examples
Examples of Table Comprehensions