• 2019.12.05【ABAP随笔】 分组循环(LOOP AT Group) / REDUCE


    ABAP 7.40新语法 LOOP AT Group 和 REDUCE

     1 *LOOP AT itab result [cond] GROUP BY key ( key1 = dobj1 key2 = dobj2 …
     2 *      [gs = GROUP SIZE] [gi = GROUP INDEX] )
     3 *      [ASCENDING|DESCENDING [AS TEXT]]
     4 *      [WITHOUT MEMBERS]
     5 *      [{INTO group}|{ASSIGNING <group>}]
     6 *
     7 *      [LOOP AT GROUP group|<group>
     8 *
     9 *      ENDLOOP.]
    10 *
    11 *ENDLOOP.
    12 *
    13 *… REDUCE type(
    14 *INIT result = start_value
    15 *
    16 *FOR for_exp1
    17 *FOR for_exp2
    18 *
    19 *NEXT …
    20 *           result = iterated_value
    21 *… )
    22 
    23 
    24 "首先创建一个内表
    25 TYPES:BEGIN OF ty_data,
    26         id       TYPE i, "人员ID
    27         name     TYPE char10, "人员名称
    28         country  TYPE char10, "国家
    29         language TYPE char2, "语言
    30         age      TYPE i,
    31       END OF ty_data,
    32       ty_t_data TYPE STANDARD TABLE OF ty_data WITH EMPTY KEY.
    33 
    34 "数据
    35 DATA(gt_data) = VALUE ty_t_data(
    36 ( id = 1 name = 'Jerry' country = 'China' language = 'ZH' age = 18 )
    37 ( id = 2 name = 'Jack' country = 'China' language = 'ZH' age = 19 )
    38 ( id = 3 name = 'Nick' country = 'Korea' language = 'EN' age = 20 )
    39 ( id = 4 name = 'Rossi' country = 'Korea' language = 'EN' age = 25 )
    40 ( id = 5 name = 'Randy' country = 'Korea' language = 'EN' age = 23 )
    41 ( id = 6 name = 'Tab' country = 'China' language = 'ZH' age = 22 )
    42 ( id = 7 name = 'Lily' country = 'Korea' language = 'EN' age = 21 )
    43 ( id = 8 name = 'Lucy' country = 'China' language = 'EN' age = 24 )
    44 ( id = 9 name = 'Zera' country = 'China' language = 'EN' age = 28 )
    45 ( id = 10 name = 'Grace' country = 'China' language = 'EN' age = 19 )
    46 )
    47 .
    48 
    49 "REDUCE
    50 "1计算年龄最大
    51 DATA(lv_age_max_zh) = REDUCE i( INIT x = 0 FOR lw_data IN gt_data
    52                       WHERE ( language = 'ZH' ) NEXT x = nmax( val1 = x
    53                                                                val2 = lw_data-age )
    54                                                                ) .
    55 WRITE:/ |说中文的人中年龄最大的是:{ lv_age_max_zh  } |.
    56 
    57 "2.输出的reduce
    58 TYPES:outref TYPE REF TO if_demo_output.
    59 DATA(output) = REDUCE outref( INIT out = cl_demo_output=>new( )
    60                               text = 'Count up:'
    61                               FOR n = 1 UNTIL n > 11
    62                               NEXT out = out->write( text )
    63                               text = | { n } | ).
    64 output->display( ).
    65 
    66 "分组循环
    67 "1.ls_data这个工作区里面是没有内容的
    68 "2.<group>里面只有size index 和分组参数
    69 LOOP AT gt_data INTO DATA(ls_data) GROUP BY ( country = ls_data-country language = ls_data-language
    70   size = GROUP SIZE index = GROUP INDEX ) ASCENDING ASSIGNING FIELD-SYMBOL(<group>).
    71 
    72   WRITE:/ |Group:{ <group>-index } Country :{ <group>-country } language : { <group>-language }| &
    73   | Number lines :{ <group>-size } |.
    74 
    75   "3.按照<group>中的分组参数循环 GT_data中的数据
    76   LOOP AT GROUP <group> ASSIGNING FIELD-SYMBOL(<ls_member>).
    77 
    78     WRITE:/ | Name:{ <ls_member>-name } |.
    79 
    80   ENDLOOP.
    81 
    82   DATA(lv_age_max) = REDUCE i( INIT max = 0 FOR lw_member IN GROUP <group>
    83                              NEXT max = nmax( val1 = max val2 = lw_member-age ) ).
    84 
    85   DATA(lv_age_min) = REDUCE i( INIT min = 100 FOR lw_member IN GROUP <group>
    86                                NEXT min = nmin( val1 = min val2 = lw_member-age ) ).
    87 
    88   DATA(lv_age_sum) = REDUCE i( INIT sum = 0 FOR lw_member IN GROUP <group>
    89                                NEXT sum = sum + lw_member-age ).
    90   DATA(lv_age_avg) = lv_age_sum / <group>-size.
    91 
    92   WRITE:/ | 该组最大年龄,最小年龄和平均年龄分别为:{ lv_age_max } { lv_age_min } { lv_age_avg } |.
    93 ENDLOOP.

    结果为:

    个人感觉LOOP AT GROUP 和 REDUCE可以很好的代替 LOOP 中使用 AT END OF field 和 AT NEW field ( 这个是需要调整内表结构字段顺序的)。

    平时用在分组求和,分组操作的时候还是很好用的。

    -TAB 热爱技术 享受生活

  • 相关阅读:
    我终于会手打lct了!
    [模板]Dijkstra-优先队列优化-单源最短路
    99999999海岛帝国后传:算法大会
    正在加载中。。。。。
    【题解】CF1054D Changing Array(异或,贪心)
    【题解】P4550 收集邮票(概率期望,平方期望)
    【题解】CF149D Coloring Brackets(区间 DP,记忆化搜索)
    【笔记】斜率优化 DP
    CSP2021 游记
    【题解】洛谷P1502 窗口的星星
  • 原文地址:https://www.cnblogs.com/jxzhu/p/11989764.html
Copyright © 2020-2023  润新知