如何将数据格式从Pipe Delimited替换为Tab Delimited?

时间:2015-11-03 06:57:58

标签: powerbuilder powerbuilder-pfc

美好的一天。下面是一个Powerbuilder脚本,用于检查文件编码并从文件中获取数据。将数据分配给字符串变量(ls_encoding)后,它将传递给对象函数of_TabDelimited()(此函数的脚本也在下面显示),以替换以制表符分隔的管道。

在对象函数of_TabDelimited()内部,应用程序挂起,我无法弄清楚PB挂起的原因。传递的数据大约是2,800行。但是如果数据不是更大的行(例如100行),则函数of_TabDelimited()正常工作。我无法在of_TabDelimited()中找到任何处理限制,因为它只在循环内执行循环和替换函数。

任何人都可以帮我找到并纠正错误?感谢任何帮助。

//Get the data from the file
ll_FileNum = FileOpen(ls_sourcepath, StreamMode!, Read!, LockWrite!, Replace!)
ll_FileLength = FileLength(ls_sourcepath)
eRet = FileEncoding(ls_sourcepath)
IF NOT ISNULL(ll_FileLength) OR ll_FileLength > 0 THEN  
    IF eRet = EncodingANSI! THEN 
        ll_bytes = FileReadEx(ll_FileNum, lbl_data)     
        ls_Encoding = String(lbl_data, EncodingUTF8!)   
        FileClose(ll_FileNum)
    END IF
ELSEIF ISNULL(ll_FileLength) OR ll_FileLength = 0 THEN 
    lb_Return = FALSE
END IF

Integer li_start, li_end
//Convert the data to TabDelimited
ls_Return = of_TabDelimited(ls_Encoding) //Application hang-up on this function.
IF NOT ISNULL(ls_Return) OR LEN(ls_Return) > 0 THEN 
    //Parse To Table
END IF


Function        : of_TabDelimited
Return Type     : String
Argument Type   : as_encoding

Long ll_start=1
String ls_old, ls_new
ls_old = "|"
ls_new = "~t"

// Find the first occurrence of old_str.
ll_start = POS(as_encoding, ls_old, ll_start)

// Only enter the loop if you find old_str.
DO WHILE ll_Start > 0
     // Replace old_str with new_str.
     as_encoding = Replace(as_encoding, ll_start, Len(ls_old), ls_new)

    // Find the next occurrence of old_str.
    ll_start = POS(as_encoding, ls_old, ll_start + Len(ls_new))
LOOP    

RETURN as_encoding

以下是我在应用程序中添加并测试的新功能。并且看起来这个脚本工作正常并且可以处理大量行。基本上这个脚本来自PFC的函数of_GlobalReplace(),来自对象n_cst_string。即使是_TabDelimited()中的脚本也取自of_GlobalReplace(),但区别在于变量ls_old和ls_new中len()的计算。

String ls_Source, ls_Old, ls_New
Long ll_Start, ll_Oldlen, ll_Newlen
ls_Old = "|"
ls_New = "~t"

//Script taken from n_cst_string - of_GlobalReplace
//Get the string lenghts
ll_OldLen = Len(ls_Old)
ll_NewLen = Len(ls_New)

//Search for the first occurrence of as_Old
ll_Start = Pos(as_source, ls_Old)

Do While ll_Start > 0
    // replace as_Old with as_New
    as_Source = Replace(as_Source, ll_Start, ll_OldLen, ls_New)

    // find the next occurrence of as_Old
    ll_Start = Pos(as_source, ls_Old, (ll_Start + ll_NewLen))
Loop

Return as_Source

1 个答案:

答案 0 :(得分:0)

我去了PFC的of_GlobalReplace()函数并从那里复制了脚本,但我已经删除了区分大小写的检查。我去测试下面的功能,它在处理文件中的大量数据时工作正常。

String ls_Source, ls_Old, ls_New
Long ll_Start, ll_Oldlen, ll_Newlen
ls_Old = "|"
ls_New = "~t"

//Script taken from n_cst_string - of_GlobalReplace
//Get the string lenghts
ll_OldLen = Len(ls_Old)
ll_NewLen = Len(ls_New)

//Search for the first occurrence of as_Old
ll_Start = Pos(as_source, ls_Old)

Do While ll_Start > 0
    // replace as_Old with as_New
    as_Source = Replace(as_Source, ll_Start, ll_OldLen, ls_New)

    // find the next occurrence of as_Old
    ll_Start = Pos(as_source, ls_Old, (ll_Start + ll_NewLen))
Loop

Return as_Source