[ABAP] Hilfsvariablen mit LET-Ausdruck definieren

LET

  • Verwendung innerhalb von Operatoren NEW, VALUE, CONV, COND, SWITCH, REDUCE, FOR …
  • es konnen inplace Variablen oder Feldsymbole deklariert werden
  • Variablen, die im LET-Ausdruck definiert wurden k?nnen nur innerhalb des jeweiligen Scopes verwendet werden

Variante 1 (Variablendefinition, CONV)

* Carrier
PARAMETERS: p_carrid TYPE scarr-carrid DEFAULT 'LH'.

START-OF-SELECTION.

* Daten holen
  SELECT * FROM scarr INTO TABLE @DATA(it_carr).

  TRY.
* String zusammenbauen
      DATA(lv_carr_name) = CONV string(
* Hilfsvariablen per LET definieren
* ersten Carrier mit Carrid holen, kann Exception CX_SY_ITAB_LINE_NOT_FOUND werfen
                                        LET <c> = it_carr[ carrid = p_carrid ]
                                            dp  = |: |
* Hilfsvariablen verwenden
                                        IN
                                        |{ <c>-carrname }{ dp }{ <c>-url }| ).

      WRITE: / lv_carr_name.

* Exceptions abfangen
    CATCH cx_root INTO DATA(e_txt).
      WRITE: / e_txt->get_text( ).
  ENDTRY.

Variante 2 (Variablendefinition, COND)

DATA(lv_lang) = COND string(
* Hilfsvariablen per LET definieren
                             LET lang     = cl_abap_syst=>get_language( )
                                 lang_iso = cl_i18n_languages=>sap1_to_sap2( im_lang_sap1 = lang )
* Hilfsvariablen verwenden
                             IN
                             WHEN lang_iso = 'DE' THEN 'Deutsch'
                             WHEN lang_iso = 'EN' THEN 'Englisch'
                             ELSE 'andere Sprache'
                           ).

WRITE: / lv_lang.

Variante 3 (Tabelleneinträge, VALUE)

TYPES: BEGIN OF ty_s_adr,
         id   TYPE i,
         city TYPE string,
       END OF ty_s_adr.

TYPES: ty_it_adr TYPE STANDARD TABLE OF ty_s_adr WITH DEFAULT KEY.

TYPES: BEGIN OF ty_s_person,
         city_id  TYPE i,
         forename TYPE string,
         surename TYPE string,
         age      TYPE i,
       END OF ty_s_person.

TYPES: ty_it_persons TYPE STANDARD TABLE OF ty_s_person WITH DEFAULT KEY.

* Städte
DATA(it_city) = VALUE ty_it_adr(
                                 ( id = 100 city = 'Berlin' )
                                 ( id = 101 city = 'Hamburg' )
                                 ( id = 102 city = 'M?nchen' )
                               ).

* Tabelle mit Personen
DATA(it_persons) = VALUE ty_it_persons(
                                        ( city_id = 100 forename = 'Udo'   surename = 'Lehmann' age = 35 )
                                        ( city_id = 101 forename = 'Erika' surename = 'Müller'  age = 63 )
                                        ( city_id = 102 forename = 'Heinz' surename = 'Schulze' age = 65 )
                                ).

DATA(it_csv) = VALUE stringtab(
* alle Namen durchgehen
                                 FOR <p> IN it_persons INDEX INTO idx
* Hilfsvariablen zusammensetzen
                                   LET name       = |{ <p>-surename }, { <p>-forename }|
                                       city       = it_city[ id = <p>-city_id ]-city
                                       age_string = COND string( WHEN <p>-age > 63 THEN 'Rentner' ELSE 'kein Rentner' )
                                       output     = |{ idx };{ name };{ city };{ age_string }|
* Hilfsvariablen verwenden
                                   IN
* neue Zeile zu Tabelle hinzufügen
                                   ( output )
                               ).

* Anzeige
cl_demo_output=>display( it_csv ).

Variante 4 (CONV)

DATA(lv_i) = CONV i( LET x = 1 IN x ).
WRITE: / lv_i.

Variante 5 (NEW)

DATA(lv_i) = NEW i( LET x = 1 IN x ).
WRITE: / lv_i->*.

[ABAP] REDUCE: Minimum / Maximum in einer internen Tabelle suchen

* Maximum
DATA(lv_max) = 100.

* Zufallszahlen 1 ... lv_max
DATA(o_rand_i) = cl_abap_random_int=>create( seed = cl_abap_random=>seed( ) min = 1 max = lv_max ).

TYPES: ty_it_rand TYPE STANDARD TABLE OF i WITH DEFAULT KEY.

* Tabelle mit Zufallswerten
DATA(it_rand) = VALUE ty_it_rand( FOR v = 1 WHILE v < 11
                                  ( o_rand_i->get_next( ) ) ).

* Datenausgabe
LOOP AT it_rand ASSIGNING FIELD-SYMBOL(<r>).
  WRITE: / sy-tabix, ':', <r>.
ENDLOOP.

* Minimumsuche
DATA(min) = REDUCE i( INIT m = lv_max
                      FOR <v> IN it_rand
                      NEXT m = COND #( WHEN <v> < m THEN <v> ELSE m ) ).

WRITE: / 'Min:', min.

* Maximumsuche
DATA(max) = REDUCE i( INIT m = 0
                      FOR <v> IN it_rand
                      NEXT m = COND #( WHEN <v> > m THEN <v> ELSE m ) ).

WRITE: / 'Max:', max.