ABAB-740 新语法
作者:桥下本有油菜花
- 2022 年 8 月 03 日
本文字数:6983 字
阅读完需:约 23 分钟
随着 ABAP 的迭代升级,产生了许多新语法,为了在以后的工作中提高工作效率,也为了能够看懂大佬写的代码,这边对新语发进行了一些总结,以便于学习和回顾.
数据声明
"Data Statement
"before
DATA text TYPE string.
text = `ABC`.
"740
DATA(text) = `ABC`.
"Loop at into work area
"before
DATA wa like LINE OF itab.
LOOP AT itab INTO wa.
…
ENDLOOP.
"740
LOOP AT itab INTO DATA(wa).
…
ENDLOOP.
"Call method
"before
DATA a1 TYPE …
DATA a2 TYPE …
oref->meth( IMPORTING p1 = a1
IMPORTING p2 = a2
).
"740
oref->meth(
IMPORTING p1 = DATA(a1)
IMPORTING p2 = DATA(a2) ).
"Loop at assigning
"before
FIELD-SYMBOLS: <line> type …
LOOP AT itab ASSIGNING <line>.
…
ENDLOOP.
"740
LOOP AT itab
ASSIGNING FIELD-SYMBOL(<line>).
…
ENDLOOP.
"Read assigning
"before
FIELD-SYMBOLS: <line> type …
READ TABLE itab
ASSIGNING <line>.
"740
READ TABLE itab
ASSIGNING FIELD-SYMBOL(<line>).
"Select into table
"before
DATA itab TYPE TABLE OF dbtab.
SELECT * FROM dbtab
INTO TABLE itab
WHERE fld1 = lv_fld1.
"740
SELECT * FROM dbtab
INTO TABLE @DATA(itab)
WHERE fld1 = @lv_fld1.
"Select single into
"before
SELECT SINGLE f1 f2
FROM dbtab
INTO (lv_f1, lv_f2)
WHERE …
WRITE: / lv_f1, lv_f2.
"740
SELECT SINGLE f1 AS my_f1,
F2 AS abc
FROM dbtab
INTO DATA(ls_structure)
WHERE …
WRITE: / ls_structure-my_f1,ls_structure-abc.
复制代码
内表操作表达式达式
*使用新语法,如果没找到则会抛出异常CX_SY_ITAB_LINE_NOT_FOUND
"Read Table index
"before
READ TABLE itab INDEX idx INTO wa.
"740
wa = itab[ idx ].
"Read Table using key
"before
READ TABLE itab INDEX idx USING KEY key INTO wa.
"740
wa = itab[ KEY key INDEX idx ].
"Read Table with key
"before
READ TABLE itab WITH KEY col1 = … col2 = … INTO wa.
"740
wa = itab[ col1 = … col2 = … ].
"Read Table with key components
"before
READ TABLE itab WITH TABLE KEY key COMPONENTS col1 = … col2 = … INTO wa.
"740
wa = itab[ KEY key col1 = … col2 = … ].
"Does record exist?
"before
READ TABLE itab … TRANSPORTING NO FIELDS.
IF sy-subrc = 0.
…
ENDIF.
"740
IF line_exists( itab[ … ] ).
…
ENDIF.
"Get table index
"before
DATA idx type sy-tabix.
READ TABLE … TRANSPORTING NO FIELDS.
idx = sy-tabix.
"740
DATA(idx) = line_index( itab[ … ] ).
复制代码
CONV 运算符
CONV dtype|#( … )
dtype = Type you want to convert to (显式转换)
# = compiler must use the context to decide the type to convert to (隐式转换)
简单理解
DATA: LV_TEXT TYPE CHAR01,
LV_STR TYPE STRING.
" 显式转换:将LV_TEXT转换成STRING类型
LV_STR = CONV STRING( LV_TEXT ).
" 隐式转换:将LV_TEXT转换成LV_STR的类型
LV_STR = CONV #( LV_TEXT ).
"before
DATA text TYPE c LENGTH 255.
DATA helper TYPE string.
DATA xstr TYPE xstring.
helper = text.
xstr = cl_abap_codepage=>convert_to( source = helper ).
"740
DATA text TYPE c LENGTH 255.
DATA(xstr) = cl_abap_codepage=>convert_to( source = CONV string( text ) ).
OR
DATA(xstr) = cl_abap_codepage=>convert_to( source = CONV #( text ) ).
复制代码
VALUE 运算符
*定义
变量:VALUE dtype|#( )
结构:VALUE dtype|#( comp1 = a1 comp2 = a2 … )
内表:VALUE dtype|#( ( … ) ( … ) … ) …
*结构赋值
TYPES: BEGIN OF ty_columns1, “Simple structure
cols1 TYPE i,
cols2 TYPE i,
END OF ty_columns1.
TYPES: BEGIN OF ty_columnns2, “Nested structure
coln1 TYPE i,
coln2 TYPE ty_columns1,
END OF ty_columns2.
DATA: struc_simple TYPE ty_columns1,
struc_nest TYPE ty_columns2.
struct_nest = VALUE t_struct(coln1 = 1
coln2-cols1 = 1
coln2-cols2 = 2
).
或者
struct_nest = VALUE t_struct(coln1 = 1
coln2 = VALUE #( cols1 = 1
cols2 = 2 )
*内表赋值
" 内表
TYPES t_itab TYPE TABLE OF i WITH EMPTY KEY.
DATA itab TYPE t_itab.
itab = VALUE #( ( ) ( 1 ) ( 2 ) ).
" Range
DATA itab TYPE RANGE OF i.
itab = VALUE #( sign = 'I' option = 'BT' ( low = 1 high = 10 )
( low = 21 high = 30 )
( low = 41 high = 50 )
option = 'GE' ( low = 61 )
)
复制代码
FOR 运算符
*定义
FOR wa|<fs> IN itab [INDEX INTO idx] [cond]
循环内表itab,读取行分配给结构wa或字段符号<fs>
INDEX为循环索引,赋值给idx变量,非必填
cond为循环条件
*案例一
TYPES: BEGIN OF TY_SHIP,
TKNUM TYPE TKNUM, "SHIPMENT NUMBER
NAME TYPE ERNAM, "NAME OF PERSON WHO CREATED THE OBJECT
CITY TYPE ORT01, "STARTING CITY
ROUTE TYPE ROUTE, "SHIPMENT ROUTE
END OF TY_SHIP.
TYPES: TY_SHIPS TYPE SORTED TABLE OF TY_SHIP WITH UNIQUE KEY TKNUM.
TYPES: TY_CITYS TYPE STANDARD TABLE OF ORT01 WITH EMPTY KEY.
" 这里的TY_SHIPS需要声明表类型,且要指明是否带KEY值
DATA(GT_SHIPS) = VALUE TY_SHIPS(
( TKNUM = 001 NAME = 'John' CITY = 'Melbourne' ROUTE = 'R0001' )
( TKNUM = 002 NAME = 'Gavin' CITY = 'Sydney' ROUTE = 'R0003' )
( TKNUM = 003 NAME = 'Lucy' CITY = 'Adelaide' ROUTE = 'R0001' )
( TKNUM = 004 NAME = 'Elaine' CITY = 'Perth' ROUTE = 'R0003' )
).
DATA(GT_CITYS) = VALUE TY_CITYS( FOR LS_SHIP IN GT_SHIPS WHERE ( ROUTE = 'R0001' )
( LS_SHIP-CITY ) ).
*案例二
TYPES: BEGIN OF TY_INDEX,
INDEX TYPE I,
NAME TYPE CHAR30,
END OF TY_INDEX.
DATA: GT_INDEX TYPE STANDARD TABLE OF TY_INDEX.
GT_INDEX = VALUE #( FOR LS_SHIP IN GT_SHIPS INDEX INTO GV_INDEX WHERE ( ROUTE = 'R0003' )
( INDEX = GV_INDEX NAME = |NAME:{ LS_SHIP-NAME }| ) ).
*案例三
TYPES: BEGIN OF TY_LINE,
COL1 TYPE I,
COL2 TYPE I,
COL3 TYPE I,
END OF TY_LINE,
TY_TAB TYPE STANDARD TABLE OF TY_LINE WITH EMPTY KEY.
DATA(GT_ITAB) = VALUE TY_TAB( FOR J = 11 THEN J + 10 UNTIL J > 40
( COL1 = J COL2 = J + 1 COL3 = J + 2 ) ).
复制代码
REDUCE 运算符
*定义
… REDUCE type(
INIT result = start_value
…
FOR for_exp1
FOR for_exp2
…
NEXT …
result = iterated_value
… )
*案例1
TYPES: BEGIN OF TY_LINE,
COL1 TYPE I,
END OF TY_LINE,
TY_TAB TYPE STANDARD TABLE OF TY_LINE WITH EMPTY KEY.
DATA(GT_ITAB) = VALUE TY_TAB( FOR J = 1 THEN J + 1 UNTIL J > 10
( COL1 = J )
).
DATA(LV_SUM) = REDUCE I( INIT X = 0 FOR LS_ITAB IN GT_ITAB NEXT X = X + LS_ITAB-COL1 ).
CL_DEMO_OUTPUT=>DISPLAY( LV_SUM ).
*案例2
YPES OUTREF TYPE REF TO IF_DEMO_OUTPUT.
DATA(OUTPUT) = REDUCE OUTREF( INIT OUT = CL_DEMO_OUTPUT=>NEW( )
TEXT = `Count up:`
FOR N = 1 THEN N + 2 UNTIL N > 11
NEXT OUT = OUT->WRITE( TEXT )
TEXT = |{ N }| ).
OUTPUT->DISPLAY( ).
复制代码
COND 操作符
*定义
… COND dtype|#( WHEN log_exp1 THEN result1
[ WHEN log_exp2 THEN result2 ]
…
[ ELSE resultn ] ) …
*案例1
DATA(TIME) =
COND STRING(
WHEN SY-TIMLO < '120000' THEN
|{ SY-TIMLO TIME = ISO } AM|
WHEN SY-TIMLO > '120000' THEN
|{ CONV T( SY-TIMLO - 12 * 3600 ) TIME = ISO } PM|
WHEN SY-TIMLO = '120000' THEN
|High Noon|
ELSE
|Throw Exception| ).
CL_DEMO_OUTPUT=>DISPLAY( TIME ).
复制代码
SWITCH 操作符
*定义
… SWITCH dtype|#( operand
WHEN const1 THEN result1
[ WHEN const2 THEN result2 ]
…
[ ELSE resultn ] ) …
*案例1
DATA(LANGU) = SWITCH #( SY-LANGU
WHEN 'D' THEN 'DE'
WHEN 'E' THEN 'EN'
WHEN '1' THEN 'ZH'
).
CL_DEMO_OUTPUT=>DISPLAY( LANGU ).
复制代码
CORRESPONDING 运算符
*定义
… CORRESPONDING type( [BASE ( base )] struct|itab [mapping|except] )
*案例1
TYPES: BEGIN OF LINE1,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE1.
TYPES: BEGIN OF LINE2,
COL1 TYPE I,
COL2 TYPE I,
COL3 TYPE I,
END OF LINE2.
DATA(LS_LINE1) = VALUE LINE1( COL1 = 1 COL2 = 2 ).
WRITE: / 'ls_line1 =' ,15 LS_LINE1-COL1, LS_LINE1-COL2.
DATA(LS_LINE2) = VALUE LINE2( COL1 = 3 COL2 = 4 COL3 = 5 ).
WRITE: / 'ls_line2 =' ,15 LS_LINE2-COL1, LS_LINE2-COL2, LS_LINE2-COL3.
SKIP 2.
LS_LINE2 = CORRESPONDING #( LS_LINE1 ).
WRITE: / 'ls_line2 = CORRESPONDING #( ls_line1 )'
,70 'Result is ls_line2 = '
,LS_LINE2-COL1, LS_LINE2-COL2, LS_LINE2-COL3.
SKIP.
LS_LINE2 = VALUE LINE2( COL1 = 3 COL2 = 4 COL3 = 5 ).
LS_LINE2 = CORRESPONDING #( BASE ( LS_LINE2 ) LS_LINE1 ).
WRITE: / 'ls_line2 = CORRESPONDING #( BASE ( ls_line2 ) ls_line1 )'
, 70 'Result is ls_line2 = '
, LS_LINE2-COL1, LS_LINE2-COL2, LS_LINE2-COL3.
SKIP.
LS_LINE2 = VALUE LINE2( COL1 = 3 COL2 = 4 COL3 = 5 ).
DATA(LS_LINE3) = CORRESPONDING LINE2( BASE ( LS_LINE2 ) LS_LINE1 ).
WRITE: / 'DATA(ls_line3) = CORRESPONDING line2( BASE ( ls_line2 ) ls_line1 )'
, 70 'Result is ls_line3 = '
, LS_LINE3-COL1, LS_LINE3-COL2, LS_LINE3-COL3.
复制代码
*Before
CLEAR ls_line2.
MOVE-CORRESPONDING ls_line1 TO ls_line2.
*740 根据line1的列数据匹配到line2上,如果不存在,则置为初始化
ls_line2 = CORRESPONDING #( ls_line1 ).
*Before
MOVE-CORRESPONDING ls_line1 TO ls_line2.
*740 相当于MOVE-CORRESPONDING 在ls_line2的基础上匹配line1的数据
ls_line2 = CORRESPONDING # ( BASE ( ls_line2 ) ls_line1 ).
*Before
DATA: ls_line3 like ls_line2.
ls_line3 = ls_line2.
MOVE-CORRESPONDING ls_line1 TO ls_line2.
*740 基于line2类型创建ls_line3,而且基于ls_line2数据的基础上,匹配line1数据
DATA(ls_line3) = CORRESPONDING line2 ( BASE ( ls_line2 ) ls_line1 ).
复制代码
字符串处理
*定义
格式:|xxx { xxx } xxx|
固定文本放在 | | 之间
变量参数放在 { } 之间
*组合 案例1
*Before
DATA lv_output TYPE string.
CONCATENATE 'Hello' 'world' INTO lv_output SEPARATED BY space.
*740
DATA(lv_out) = |Hello| & | | & |world|.
*宽度/对齐方式/填充
WRITE / |{ 'Left' WIDTH = 20 ALIGN = LEFT PAD = '0' }|.
WRITE / |{ 'Centre' WIDTH = 20 ALIGN = CENTER PAD = '0' }|.
WRITE / |{ 'Right' WIDTH = 20 ALIGN = RIGHT PAD = '0' }|.
*大小写
WRITE / |{ 'Text' CASE = (cl_abap_format=>c_raw) }|.
WRITE / |{ 'Text' CASE = (cl_abap_format=>c_upper) }|.
WRITE / |{ 'Text' CASE = (cl_abap_format=>c_lower) }|.
*前导零
DATA(lv_vbeln) = '0000012345'.
WRITE / |{ lv_vbeln ALPHA = OUT }|. “or ALPHA = IN to go in other direction
*日期
WRITE / |{ pa_date DATE = ISO }|. “Date Format YYYY-MM-DD
WRITE / |{ pa_date DATE = User }|. “As per user settings
WRITE / |{ pa_date DATE = Environment }|. “As per Environment
复制代码
Loop at
*定义
LOOP AT itab result [cond] GROUP BY key ( key1 = dobj1 key2 = dobj2 …
[gs = GROUP SIZE] [gi = GROUP INDEX] )
[ASCENDING|DESCENDING [AS TEXT]]
[WITHOUT MEMBERS]
[{INTO group}|{ASSIGNING <group>}]
…
[LOOP AT GROUP group|<group>
…
ENDLOOP.]
…
ENDLOOP.
*案例1
TYPES: BEGIN OF TY_PEOPLE,
NAME TYPE CHAR30,
AGE TYPE I,
SEX TYPE CHAR2,
ROLE TYPE CHAR10,
END OF TY_PEOPLE.
TYPES: TY_PEOPLE_T TYPE STANDARD TABLE OF TY_PEOPLE WITH KEY NAME.
DATA: LV_AGE_SUM TYPE I,
LV_AGE_AVG TYPE P DECIMALS 3.
DATA(LT_PEOPLES) = VALUE TY_PEOPLE_T(
( NAME = '张三' AGE = 11 SEX = '男' ROLE = '学生')
( NAME = '李四' AGE = 12 SEX = '女' ROLE = '老师')
( NAME = '王五' AGE = 13 SEX = '男' ROLE = '学生')
( NAME = '赵六' AGE = 14 SEX = '女' ROLE = '打工人')
( NAME = '孙七' AGE = 15 SEX = '男' ROLE = '老师')
( NAME = '吴八' AGE = 16 SEX = '女' ROLE = '学生')
( NAME = '陈九' AGE = 17 SEX = '男' ROLE = '打工人')
( NAME = '华十' AGE = 18 SEX = '女' ROLE = '学生')
).
"这里的排序是根据关键字来排序的 先根据ROLE,相同的ROLE再根据SEX
LOOP AT LT_PEOPLES REFERENCE INTO DATA(LS_PEOPLES)
GROUP BY ( ROLE = LS_PEOPLES->ROLE
SEX = LS_PEOPLES->SEX
SIZE = GROUP SIZE
INDEX = GROUP INDEX )
DESCENDING
INTO DATA(LT_GROUPS).
CLEAR: LV_AGE_SUM.
WRITE: / |Group: { LT_GROUPS-INDEX }
Role: { LT_GROUPS-ROLE WIDTH = 15 }
Sex: { LT_GROUPS-SEX WIDTH = 10 }
Number in this role: { LT_GROUPS-SIZE }|.
LOOP AT GROUP LT_GROUPS INTO DATA(LS_GROUPS).
LV_AGE_SUM = LV_AGE_SUM + LS_GROUPS-AGE.
WRITE: /25 LS_GROUPS-NAME.
ENDLOOP.
LV_AGE_AVG = LV_AGE_SUM / LT_GROUPS-SIZE.
WRITE: / |Average age: { LV_AGE_AVG }|.
SKIP.
ENDLOOP.
复制代码
Filter 运算符
*定义
… FILTER type( itab [EXCEPT] [IN ftab] [USING KEY keyname]
WHERE c1 op f1 [AND c2 op f2 […]] )
*案例
TYPES: BEGIN OF TY_FILTER,
CITYFROM TYPE SPFLI-CITYFROM,
CITYTO TYPE SPFLI-CITYTO,
F3 TYPE I,
END OF TY_FILTER,
TY_FILTER_TAB TYPE HASHED TABLE OF TY_FILTER
WITH UNIQUE KEY CITYFROM CITYTO.
DATA: LT_SPLFI TYPE STANDARD TABLE OF SPFLI.
SELECT * FROM SPFLI APPENDING TABLE LT_SPLFI.
DATA(LT_FILTER) = VALUE TY_FILTER_TAB( F3 = 2
( CITYFROM = 'NEW YORK' CITYTO = 'SAN FRANCISCO' )
( CITYFROM = 'FRANKFURT' CITYTO = 'NEW YORK' ) ).
DATA(LT_MYRECS) = FILTER #( LT_SPLFI IN LT_FILTER
WHERE CITYFROM = CITYFROM
AND CITYTO = CITYTO ).
"OUTPUT FILTERED RECORDS
WRITE: / 'Carrid', 8 'CityFrom', 30 'CityTo', 45 'Deptime'.
LOOP AT LT_MYRECS ASSIGNING FIELD-SYMBOL(<LS_REC>).
WRITE: / <LS_REC>-CARRID,8 <LS_REC>-CITYFROM,30
<LS_REC>-CITYTO,45 <LS_REC>-DEPTIME.
ENDLOOP.
复制代码
转载于:https://blog.csdn.net/wx774891/article/details/122364453
划线
评论
复制
发布于: 刚刚阅读数: 3
桥下本有油菜花
关注
还未添加个人签名 2022.06.08 加入
还未添加个人简介
评论