ABAP 新语法-ITAB[ idx ]、ITAB[ key ]

这段ABAP代码主要演示了使用新的ABAP语法内表表达式的用法,其中核心点如下:

索引和关键字读取:

使用gt_student[1]进行索引读取,获取内表的第一个元素。使用gt_student[id = '0000000005' age = 15]进行关键字读取,根据指定条件获取内表中的元素。

结构字段读取:

通过gt_student[1]-namegt_student[id = '0000000002']-name的方式,直接从内表元素中读取结构字段的值。

避免抛出异常的几种方式:

使用ASSIGN gt_student[id = '0000000003' name = 'Student 3'] TO FIELD-SYMBOL(<stu>),通过ASSIGN语句避免抛出异常。

条件表达式和行是否存在的检查:

使用line_exists(gt_student[1])检查内表中是否存在指定的行。

内表表达式的默认值和OPTIONAL选项:

使用VALUE #(gt_student[11] OPTIONAL),通过OPTIONAL选项在索引越界时避免抛出异常。使用VALUE #(gt_student[11] DEFAULT VALUE #(id = '0000000010' name = 'Student 10')),在索引越界时提供默认值。

使用条件表达式处理异常情况:

使用COND表达式,例如COND #(LET c = lines(gt_student) IN WHEN c > 10 THEN gt_student[11]),在特定条件下选择内表中的元素。

使用TRY-CATCH处理异常:

使用TRY...CATCH语句,捕获特定的异常类型,这里是cx_sy_itab_line_not_found

 TYPES:
    BEGIN OF gty_student,
      name(10)  TYPE c,            "姓名
      gender    TYPE c,            "性别
      age       TYPE i,            "年龄
      id(10)    TYPE n ,           "学生ID
      class(10) TYPE c,            "班级
      score     TYPE p DECIMALS 1, "成绩
      level(10) TYPE c,            "级别
      ispass    TYPE c,            "是否通过
    END OF gty_student.

  DATA:
    gt_student TYPE TABLE OF gty_student,
    gs_student TYPE gty_student.

  gt_student = VALUE #( LET pre = 'Student'
                            random = cl_abap_random_int=>create( seed = CONV #( sy-timlo ) min = 0 max = 100 ) IN
                        FOR n = 1 UNTIL n > 10
                          ( id = n
                            name = |{ pre } { n }|
                            gender = COND #( WHEN n < 6 THEN '男'
                                             ELSE '女' )
                            age = 10 + n
                            class = |{ n }班|
                            score = random->get_next( ) ) ).


*&----------------------------------------------------------------------
*& 2. 内表表达式                      理解:内表表达式,相当于Read Table
*&----------------------------------------------------------------------
*& 语法:itab[ idx ].
*&       itab[ col1 = … col2 = … ].
*&----------------------------------------------------------------------
  "不存在会抛出异常(cx_sy_itab_line_not_found)
  IF 1 <> 1.
    DATA(ls_index) = gt_student[ 1 ]. "index读取
    DATA(ls_key) = gt_student[ id = '0000000005' age = 15  ]. "key读取

    DATA(lv_index_name) = gt_student[ 1 ]-name. "index读取结构字段
    DATA(lv_key_name) = gt_student[ id = '0000000002' ]-name. "key读取字段
  ENDIF.

  "避免抛出异常的几种方式
  ASSIGN gt_student[ id = '0000000003' name = 'Student 3'  ] TO FIELD-SYMBOL(<stu>).
  IF sy-subrc EQ 0.
  ENDIF.

  IF line_exists( gt_student[ 1 ] ).
    DATA(ls_stu01) = gt_student[ 1 ].
    DATA(lv_name) = gt_student[ 1 ]-name.
  ENDIF.

  DATA(ls_stu02) = VALUE #( gt_student[ 11 ] OPTIONAL ).

  DATA(ls_stu03) = VALUE #( gt_student[ 11 ] DEFAULT VALUE #( id = '0000000010' name = 'Student 10' ) ).

  DATA(ls_stu04) = VALUE #( gt_student[ 11 ] DEFAULT VALUE #( gt_student[ 10 ] OPTIONAL ) ).

  DATA(ls_stu05) = COND #( LET c = lines( gt_student ) IN WHEN c > 10 THEN gt_student[ 11 ] ).

  DATA(ls_stu06) = COND #( WHEN line_exists( gt_student[ 11 ] ) THEN gt_student[ 11 ] ).

  TRY .
      DATA(ls_stu07) = gt_student[ 11 ].
    CATCH cx_sy_itab_line_not_found.
*  WRITE : 'Not Found!'.
  ENDTRY.

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ᝰ随心ꦿེএ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值