在数组上拆分函数 [英] Split function on an array

查看:26
本文介绍了在数组上拆分函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有多个分隔符的大文本文件(1 长行)(例如:~*:).~ 分隔符标记一个新的节,*: 分隔符标记子节或段.

I have a large text file (1 long line) with multiple delimiters (eg: ~, *, :). The ~ delimiter marks a new section, and the * and : delimiters mark sub sections or segments.

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

I tried the following but I'm getting a type mismatch error, likely because the Split function is meant to be used on a string, not an array.

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

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

I'm trying to use this logic to keep my segments and segment fields with the correct section, and insert those value into a database.

知道如何使用 VBScript 完成此任务吗?

Any idea on how I can accomplish this with VBScript?

推荐答案

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

The solution depends on how the fields are to be imported into the database. If you simply want to process all fields in the order they appear in the input file, you could replace the separator characters with newlines and then split the string at the newlines:

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

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

If you need to put more emphasis on the structure of the input file, you could process the input string with nested loops:

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 已经指出的那样,您需要提供更多关于如何将层次结构导入数据库的信息,以获得进一步的帮助.

For further assistance you'd need to supply more information about how the hierarchical structure should be imported into the database, as Ekkehard.Horner already pointed out.

这篇关于在数组上拆分函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆