用于拆分分隔符字符串的代码

时间:2016-11-13 22:04:32

标签: progress-4gl openedge

我是学习Progress OpenEdge的新手。我有一个关于如何从用户拆分字符串输入以在一个过程中使用分隔符获取输出的问题。

例如,

输入

"A0020000A103A0A0A0A501A4A405A5A5A5A5A5"   

输出应为:

HEADER  LEN  DATA
-------------   ------   --------------
A0              02   0000
A1              03   A0A0A0
A5              01   A4
A4              05   A5A5A5A5A5

或者

输入:

"B103X0X0X0C204B1B1B1B1A209C2C2C2C2C2C2C2C2C2X301A2"

输出:

HEADER  LEN  DATA
-------------   ------   -----------------------
B1              03    X0X0X0
C2              04    B1B1B1B1
A2              09    C2C2C2C2C2C2C2C2C2
X3              01    A2

2 个答案:

答案 0 :(得分:1)

我试图从您的输出中反向设计您的要求。

看起来数据是两个字符的块。一个“标题”后跟一个“计数器”字段,然后是计数器指示的许多双字符“数据”字段。

这似乎以你想要的方式打破了字符串:

define variable data as character no-undo format "x(60)".

function getElements returns integer ( input str as character ):
  return integer( substring( str, 1, 2 )).
end.


function getData returns character ( input str as character ):
  return substring( str, 3, getElements( str ) * 2 ).
end.

procedure parse:

  define input parameter str as character no-undo.

  define variable ii as integer no-undo.
  define variable jj as integer no-undo.

  define variable nn as integer no-undo.

  nn = length( str ).
  ii = 1.

  do while ii < nn:

    display
      substring( str, ii, 2 )
      substring( str, ii + 2, 2 )
    .

    data = getData( substring( str, ii + 2 )).

    display data.

    pause.

    ii = ii + 4 + length( data ).

  end.

  return.

end.

run parse ( "A0020000A103A0A0A0A501A4A405A5A5A5A5A5" ).
run parse ( "B103X0X0X0C204B1B1B1B1A209C2C2C2C2C2C2C2C2C2X301A2" ).

return.

答案 1 :(得分:1)

与汤姆的答案相同,但没有功能和显示格式。

procedure parse:
  define input parameter i_c as character no-undo.

  def var itotal  as int  no-undo.
  def var ipos    as int  no-undo initial 1.

  def var cheader as char no-undo label "Header" format "x(2)".
  def var ilen    as int  no-undo label "Len"    format "99".
  def var cdata   as char no-undo label "Data"   format "x(50)".

  itotal = length( i_c ).

  repeat while ipos < itotal:

    assign
      cheader = substring( i_c, ipos, 2 )
      ilen    = integer( substring( i_c, ipos + 2, 2 ) )
      cdata   = substring( i_c, ipos + 4, 2 * ilen )            
      ipos    = ipos + 4 + ilen * 2
      .

    display
      cheader
      ilen 
      cdata
    with width 70.

  end.

end procedure.

run parse ( "A0020000A103A0A0A0A501A4A405A5A5A5A5A5" ).
run parse ( "B103X0X0X0C204B1B1B1B1A209C2C2C2C2C2C2C2C2C2X301A2" ).
  • 使用带有标签和格式的变量
  • repeat具有块处理功能,以便所有记录显示在彼此之下
  • 我完全清楚汤姆会抱怨变量名数据类型前缀,但他们击败了ii,jj和nn的地狱,使用骚扰名称来避免与进度关键字冲突是不值得的(imho)< / LI>