[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.