Inline Decleration

DATA TYPE declarations is the first thing we do when we start ABAP coding. We declare the variables at the start of the subroutine, method and report program etc. With new syntax data can be defined easily, below are some of the example

*DATA(lv_r) = 'TX'.
READ TABLE lt_mara INTO DATA(lw_mara) INDEX 1.
SELECT matnr 
       ersda 
       ernam 
       laeda 
       aenam FROM mara 
             INTO TABLE @DATA(lt_mara) UP TO 10 ROWS.

Ways to loop through an internal table

We have multiple options here to create work area and also it can be created inline.

Using a simple work area of table structure type :

LOOP AT gt_loop INTO DATA(ls_work_area).
ENDLOOP.

Using work area of field symbol type :

LOOP AT gt_loop ASSIGNING FIELD–SYMBOL(<fs_field_sy>).
ENDLOOP.

Using work area of object type :

LOOP AT gt_loop REFERENCE INTO DATA(lo_obj).
ENDLOOP.

Below is a sample program which shows simple ABAP program with new syntax

Define structure
TYPES:
  BEGIN OF ty_struct1,
    field1 TYPE i,
    field2 TYPE string,
  END OF ty_struct1,
  BEGIN OF ty_struct2,
    field1 TYPE i,
    field2 TYPE string,
    field3 TYPE i,
  END OF ty_struct2.

*Define table types
TYPES:  gtt_struct1 TYPE STANDARD TABLE OF ty_struct1 WITH DEFAULT KEY,
        gtt_struct2 TYPE STANDARD TABLE OF ty_struct2 WITH DEFAULT KEY.

* Initialize the values
DATA(lt_source) = VALUE gtt_struct1(
    ( field1 = 1 field2 = 'A' )
    ( field1 = 2 field2 = 'B' ) ).

*WRITE:/ 'TEST'.

*move corresponding
DATA(lt_target1) = VALUE gtt_struct2( FOR lwa_source IN lt_source ( CORRESPONDING #( lwa_source ) ) ).
cl_demo_output=>display( lt_target1 ).

*Populate sy-tabix in the additional fields within the for loop
DATA(lt_target2) = VALUE gtt_struct2( FOR lwa_source IN lt_source
                            INDEX INTO index
                            LET base = VALUE ty_struct2( field3 = index )
                            IN ( CORRESPONDING #( BASE ( base ) lwa_source ) ) ).
cl_demo_output=>display( lt_target2 ).


cl_demo_output=>display( lt_target1 ).

Some points to note in the above program

See the way we are initializing the values, so on the above we are creating an internal table lt_source which is of type gtt_struct1

* Initialize the values
 DATA(lt_source) = VALUE gtt_struct1(     
( field1 = 1 field2 = 'A' )     ( field1 = 2 field2 = 'B' ) ).

move corresponding

*move corresponding DATA(lt_target1) = VALUE gtt_struct2( FOR lwa_source IN lt_source ( CORRESPONDING #( lwa_source ) ) ).

populating additional fields using LET

So what is LET ?

LET is a new syntax keyword introduced in ABAP 7.4. This keyword is used for local declarations in constructor expressions. In the same way the LET keyword is also used to declare variables with short lifespans by getting us relieved from declaring helper variables. LET expressions can be used in the following constructor expressions

  1. NEW
    • Single Values
    • Structures
    • Internal Tables
    • Classes
  2. VALUE
    • Structures
    • Internal Tables
  3. CONV
  4. CAST
  5. EXACT
  6. REDUCE
  7. COND
  8. SWITCH

Apart from the above constructor expressions the LET expressions can be used in all iteration expressions with FOR.

*Populate sy-tabix in the additional fields within the for loop DATA(lt_target2) = VALUE gtt_struct2( FOR lwa_source IN lt_source                             INDEX INTO index                             LET base = VALUE ty_struct2( field3 = index )                             IN ( CORRESPONDING #( BASE ( base ) lwa_source ) ) ). cl_demo_output=>display( lt_target2 ).

 VALUE (Type of the internal table) is used for the in-line declaration of the internal table. Here structure and Type declaration is required before using VALUE operator. If you want to use VALUE # then type of the table must be already available and defined in the program i.e. the internal table need to be declared before.

Below blog explains it in detail

https://sapbazar.com/articles/item/4328-new-advanced-abap-74-and-above-part-4-loops-and-meshes