以前写循环时,都是一条记录一条记录的比较,代码示例如下:
loop at S_VAL_CHAR into VAL_CHAR.
if VAL_CHAR-CHARACT eq 'P0008_7'."箱型
IT_ITEM-BOX = VAL_CHAR-VALUE_CHAR.
ELSEIF VAL_CHAR-CHARACT eq 'S0012'."客户编号
IT_ITEM-CLIENT_CODE = VAL_CHAR-VALUE_CHAR.
ELSEIF VAL_CHAR-CHARACT eq 'CP_CW'."客户编号
IT_ITEM-CP_CW = VAL_CHAR-VALUE_CHAR.
ENDIF.
ENDLOOP.
这样的话要循环lines( S_VAL_CHAR ).
应该改成如下,效率应该会好一点.
loop at S_VAL_CHAR into VAL_CHAR WHERE CHARACT eq 'P0008_7' OR CHARACT eq 'S0012' OR CHARACT eq 'CP_CW'.
if VAL_CHAR-CHARACT eq 'P0008_7'."箱型
IT_ITEM-BOX = VAL_CHAR-VALUE_CHAR.
ELSEIF VAL_CHAR-CHARACT eq 'S0012'."客户编号
IT_ITEM-CLIENT_CODE = VAL_CHAR-VALUE_CHAR.
ELSEIF VAL_CHAR-CHARACT eq 'CP_CW'."客户编号
IT_ITEM-CP_CW = VAL_CHAR-VALUE_CHAR.
ENDIF.
ENDLOOP.
若条件很多,条件语句写起来太长,可用range,代码如下:
data:lr_character type range of BAPI1003_ALLOC_VALUES_CHAR-CHARACT,
lr_character_line like line of lr_character.
CLEAR: lr_character_line,lr_character[].
lr_character_line = 'IEQ'.
lr_character_line-low = 'P0008_7'.
append lr_character_line to lr_character.
CLEAR: lr_character_line.
lr_character_line = 'IEQ'.
lr_character_line-low = 'S0012'.
append lr_character_line to lr_character.
CLEAR: lr_character_line.
lr_character_line = 'IEQ'.
lr_character_line-low = 'CP_CW'.
append lr_character_line to lr_character.
loop at S_VAL_CHAR into VAL_CHAR WHERE CHARACT in lr_character.
if VAL_CHAR-CHARACT eq 'P0008_7'."箱型
IT_ITEM-BOX = VAL_CHAR-VALUE_CHAR.
ELSEIF VAL_CHAR-CHARACT eq 'S0012'."客户编号
IT_ITEM-CLIENT_CODE = VAL_CHAR-VALUE_CHAR.
ELSEIF VAL_CHAR-CHARACT eq 'CP_CW'."客户编号
IT_ITEM-CP_CW = VAL_CHAR-VALUE_CHAR.
ENDIF.
ENDLOOP.
继续优化一下,把那些重复的代码提取出来,最后如下:
data: lr_character type range of BAPI1003_ALLOC_VALUES_CHAR-CHARACT,
lr_character_line like line of lr_character.
form ADD_CHARACTER_TO_RANGE USING value(s) CHANGING r.
clear lr_character_line.
lr_character_line = 'IEQ'.
lr_character_line-low = s.
append lr_character_line to lr_character.
ENDFORM.
CLEAR: lr_character[].
perform ADD_CHARACTER_TO_RANGE using 'P0008_7' changing lr_character.
perform ADD_CHARACTER_TO_RANGE using 'S0012' changing lr_character.
perform ADD_CHARACTER_TO_RANGE using 'CP_CW' changing lr_character.
loop at S_VAL_CHAR into VAL_CHAR WHERE CHARACT in lr_character.
if VAL_CHAR-CHARACT eq 'P0008_7'."箱型
IT_ITEM-BOX = VAL_CHAR-VALUE_CHAR.
ELSEIF VAL_CHAR-CHARACT eq 'S0012'."客户编号
IT_ITEM-CLIENT_CODE = VAL_CHAR-VALUE_CHAR.
ELSEIF VAL_CHAR-CHARACT eq 'CP_CW'."客户编号
IT_ITEM-CP_CW = VAL_CHAR-VALUE_CHAR.
ENDIF.
ENDLOOP.