VALUE - Internal Tables
7.31 | 7.40 | 7.54
... VALUE dtype|#( [let_exp]
( line_spec1 )
( line_spec2 )
... ) ...
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
LETexpression 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
- 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 in
line_specand inserts them into the new internal table in accordance with the rules for the statement INSERT ... INTO TABLE, with one difference: Any conflicts with an existing unique primary table key raise an exception of the class CX_SY_ITAB_DUPLICATE_KEY. 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
x), in which case it is padded on the right with blanks or hexadecimal 0.
VALUE operator is used as the source of an assignment to an internal
table, this table is first initialized after the evaluation of the
LET expression (if available) or is assigned the content of
line_spec data is then evaluated and inserted directly in the target table.
- In assignments of constructor expressions to an internal table, its existing rows cannot be used
directly as an argument in
line_spec. This is because this table is deleted before
line_specis evaluated or overwritten by the content of
itab. If the entire internal table or rows from the left side are needed on the right side, however, they can be saved in local helper variables using a
LETexpression, since this expression is evaluated first.
- The operand type for
line_specin the inner parentheses is the row type of the table type specified using
#, which means it is always uniquely identifiable. Constructor expressions in this place can therefore always derive the required data type using
Constructs an internal table with a structured row type and fills it with two rows. The structures are filled with values component by component.
TYPES: BEGIN OF t_struct, col1 TYPE i, col2 TYPE i, END OF t_struct, t_itab TYPE TABLE OF t_struct WITH EMPTY KEY. DATA itab TYPE t_itab. itab = VALUE #( ( col1 = 1 col2 = 2 ) ( col1 = 3 col2 = 4 ) ).
Constructs an internal table with a tabular row type and fills it with two rows. The first row is assigned a table that is already filled. The second row is constructed using
TYPES: t_itab1 TYPE TABLE OF i WITH EMPTY KEY, t_itab2 TYPE TABLE OF t_itab1 WITH EMPTY KEY. DATA itab1 TYPE t_itab1. DATA itab2 TYPE t_itab2. itab1 = VALUE #( ( 1 ) ( 2 ) ( 3 ) ). itab2 = VALUE #( ( itab1 ) ( VALUE t_itab1( ( 4 ) ( 5 ) ( 6 ) ) ) ).
See also the examples for the instance operator
... BASE itab
BASE, followed by an internal table,
itab, can be specified in front of the lines that you want to insert. This is a
position. The row type of
itab must be convertible to the row type of
the return value. If
BASE is specified, the content of
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).
- If the addition
BASEis not specified, the content of tables can only be reconstructed using the value operator and no new content can be added. If the same table is specified after
BASEto which the constructor expression is assigned, further rows can be inserted in this table.
- If the target table is specified as
BASEin an assignment to an existing internal table, no assignment takes place before
line_specis evaluated, and the target table just keeps its value instead.
BASE. The table type of
is applied in the construction of
This is not possible in the construction of
tab3 since the row type of
base2 is not structured and hence not suitable for specifying individual components in the parentheses
that follow. The type
itab2 is specified explicitly for
This is possible since the row type of
base2 can be converted into this row type. Sorted tables are constructed, which means that the rows in the results are also sorted.
TYPES: itab1 TYPE SORTED TABLE OF string WITH UNIQUE KEY table_line, BEGIN OF struct, col1 TYPE c LENGTH 2, col2 TYPE c LENGTH 2, col3 TYPE c LENGTH 2, END OF struct, itab2 TYPE SORTED TABLE OF struct WITH UNIQUE KEY col1 col2 col3. DATA(base1) = VALUE itab1( ( `x1y1z1` ) ( `x2y2z2` ) ( `x3y3z3` ) ). DATA(base2) = VALUE itab2( ( col1 = 'x1' col2 = 'y1' col3 = 'z1' ) ( col1 = 'x2' col2 = 'y2' col3 = 'z2' ) ( col1 = 'x3' col2 = 'y3' col3 = 'z3' ) ). DATA(tab1) = VALUE #( BASE base1 ( `A1B1B1` ) ( `A2B2B2` ) ). DATA(tab2) = VALUE #( BASE base2 ( col1 = 'A1' col2 = 'B1' col3 = 'C1' ) ( col1 = 'A2' col2 = 'B2' col3 = 'C2' ) ). DATA(tab3) = VALUE itab2( BASE base1 ( col1 = 'A1' col2 = 'B1' col3 = 'C1' ) ( col1 = 'A2' col2 = 'B2' col3 = 'C2' ) ). cl_demo_output=>write( tab1 ). cl_demo_output=>write( tab2 ). cl_demo_output=>display( tab3 ).
BASE to append rows to existing rows in an internal table.
TYPES itab TYPE TABLE OF string WITH EMPTY KEY. DATA(itab) = VALUE itab( ( `a` ) ( `b` ) ( `c` ) ). ... itab = VALUE #( BASE itab ( `d` ) ( `e` ) ( `f` ) ). cl_demo_output=>display( itab ).
BASE to append rows to an internal table in a loop. After the output,
it possible to see how the same function can be applied using an iteration expression with
DATA itab TYPE TABLE OF i WITH EMPTY KEY. DO 10 TIMES. itab = VALUE #( BASE itab ( ipow( base = sy-index exp = 2 ) ) ). ENDDO. cl_demo_output=>display( itab ). DATA jtab LIKE itab. jtab = VALUE #( FOR j = 1 UNTIL j > 10 ( ipow( base = j exp = 2 ) ) ). ASSERT jtab = itab.
... FOR for_exp
If one or more iteration
expressions are specified consecutively using
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.
- The term "table comprehension" is derived from similar concepts used in many other languages, where it is also known as "list comprehension".
- Conditional iterations and table comprehensions are also possible with
FORexpressions for mesh paths.
- For tasks that can be solved using both table comprehensions and
special assignments for components (in particular the
CORRESPONDING), it is best to use assignments (see the executable example).
- If the
VALUEoperator is used, it should be noted that assignments to internal tables are also initialized (or given the content of
BASE) even in the case of table comprehensions after the evaluation of a
LETexpressions. The target table is then used directly. The original table can therefore not be used directly in the
FORexpressions (unless it is assigned to a helper variable after
Constructs a temporary 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. cl_demo_output=>display( VALUE t_itab( FOR i = 1 UNTIL i > 10 ( ipow( base = i exp = 2 ) ) ) ).