在数组上拆分函数

时间:2012-12-31 15:46:43

标签: vbscript

我有一个包含多个分隔符的大文本文件(1个长行)(例如:~*:)。 ~分隔符标记新部分,*:分隔符标记子部分或细分。

我尝试了以下操作,但是我遇到了类型不匹配错误,可能是因为Split函数用于字符串而不是数组。

Dim  strFileLine, arrSection, arrSegment, arrSegField
strFileLine = "C:\sometextfile.txt"
arrSection  = Split(strFileLine, "~")
arrSegment  = Split(arrSection, "*")
arrSegField = Split(arrSegment, ":")

我正在尝试使用此逻辑将我的段和段字段保留为正确的部分,并将这些值插入数据库。

有关如何使用VBScript实现此目的的任何想法吗?

3 个答案:

答案 0 :(得分:5)

解决方案取决于如何将字段导入数据库。如果您只想按照它们在输入文件中出现的顺序处理所有字段,您可以用换行符替换分隔符,然后在换行符处拆分字符串:

Set fso = CreateObject("Scripting.FileSystemObject")

Set text = fso.OpenTextFile("C:\sometextfile.txt").ReadAll

text = Replace(text, "~", vbNewLine)
text = Replace(text, "*", vbNewLine)
text = Replace(text, ":", vbNewLine)

arr = Split(text, vbNewLine)

For Each field In arr
  WScript.Echo field
Next

如果您需要更多地强调输入文件的结构,可以使用嵌套循环处理输入字符串:

Set fso = CreateObject("Scripting.FileSystemObject")

Set text = fso.OpenTextFile("C:\sometextfile.txt").ReadAll

For Each segment In Split(text, "~")
  For Each section In Split(segment, "*")
    For Each field In Split(section, ":")
      WScript.Echo field
    Next
  Next
Next

如需进一步的帮助,您需要提供有关如何将层次结构导入数据库的更多信息,正如Ekkehard.Horner已经指出的那样。

答案 1 :(得分:0)

        Dim strFileLine
    Dim arrSection()
    Dim arrSegment()
    Dim arrSegField()
    Dim strBuf 
    Dim counta 
    Dim character 
    dim sectioncount 
    dim segmentcount 
    dim segfieldcount 

    strFileLine = "aaaaa~00000?AAAAA:bbbbb~11111?BBBBB:ccccc~22222?CCCCC:ddddd~33333?DDDDD:eeeee~44444?EEEEE:fffff~55555?EEEEE:"
    strBuf = ""
    For counta = 1 To Len(Trim(strFileLine))
        character = Mid(strFileLine, counta, 1)
        Select Case character
            Case "~"
                sectioncount = sectioncount + 1
                redim preserve arrSection(sectioncount - 1)
                arrSection(sectioncount-1)=strBuf
                strBuf = ""
            Case "?"
                segmentcount = segmentcount + 1
                redim preserve arrSegment(segmentcount - 1)
                arrSegment(segmentcount-1)=strBuf
                strBuf = ""
            Case ":"
                segfieldcount = segfieldcount + 1
                redim preserve arrSegField(segfieldcount - 1)
                arrSegField(segfieldcount-1)=strBuf
                strBuf = ""
            Case Else
                strBuf = strBuf & character
        End Select
    Next
    For counta = 0 To ubound(arrSection)-1
        document.Write("SECTION:=" & arrSection(counta) & "<br/>")
    Next
    For counta = 0 To ubound(arrSegment)-1
        document.Write("SEGMENT:=" & arrSegment(counta) & "<br/>")
    Next
    For counta = 0 To ubound(arrSegField)-1
        document.Write("SEGFIELD:=" & arrSegField(counta) & "<br/>")
    Next

答案 2 :(得分:0)

要获得Zaf解决方案中缺少的第五部分(第5部分/第5部分/第5部分字段),必须从FOR循环中删除-1。例如:

redim preserve arrSection(sectioncount)
arrSection(sectioncount) = strBuf
sectioncount = sectioncount + 1
strBuf = ""

For counta = 0 To ubound(arrSection)   'REMOVE -1 from here
    document.Write("SECTION " & counta & " : " & arrSection(counta) & "<br>")
Next

(Segment&amp; Segfield也是如此)。 (另外:strFileLine(EEEEE :)中的最后6个字符应该读为FFFFF :) 无论如何,Zaf很好地表示,使用小数据,Redim Preserve非常快! (他的解决方案可以保存为HTM文件进行测试(如果使用IE,则在IE10模式下))