这个数据处理的函数怎么写?线上等待

qozm 2003-11-25 08:19:46
初始字符串
1:G101:20|1:G02:12|1:G103:11|2:G104:19|2:G106:24|3:G105:43

说明:分别用 | 隔开的是几个小组,
每个小组有三个数据,用:分隔开,
第一个数是小组编号,第二个是组的名称,第三个是小组的人数

我需要把编号相同的小组合在一起,得到最后结果为
1:G101,G102,G103:43|2:G104,G106:43|3:G105:43

说明:结果把相同编号的小组合成了一个大组,
每个大组也包括三个数据,用 : 分隔开,
第一个是大组编号,第二个是包括的小组名称,用逗号隔开,第三个是相同编号的小组的人数累加。

我需要在vb中通过程序处理,这个处理函数怎么写?


我用split来处理,结果弄了一天搞的头晕脑胀,
谁能给我写一个完整的函数出来?
...全文
51 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
northwolves 2003-11-25
  • 打赏
  • 举报
回复
粘贴错误。
Function group(ByVal init As String) As String
On Error Resume Next
Dim dazu As New Collection
Dim a() As String, b() As Long, c() As String
temp = Split(init, "|")
For i = 0 To UBound(temp)
dazu.Add Split(temp(i), ":")(0), "group" & Split(temp(i), ":")(0)
Next
ReDim a(dazu.Count - 1), b(dazu.Count - 1), c(dazu.Count - 1)
For j = 0 To dazu.Count - 1
For i = 0 To UBound(temp)

If Split(temp(i), ":")(0) = dazu(j + 1) Then
a(j) = a(j) & "," & Split(temp(i), ":")(1)

b(j) = b(j) + Val(Split(temp(i), ":")(2))
End If
Next
If Left(a(j), 1) = "," Then a(j) = Right(a(j), Len(a(j)) - 1)

c(j) = dazu(j + 1) & ";" & a(j) & ":" & b(j)
Next
Set dazu = Nothing
Erase a
Erase b
group = Join(c, "|")
Erase c
End Function


Private Sub Command1_Click()
Const x = "1:G101:20|1:G02:12|1:G103:11|2:G104:19|2:G106:24|3:G105:43|4:G106:15|5:G103:24|3:G101:41"
MsgBox group(x)
End Sub

用DICTIONARY更易操作,只是多了一个引用。
simanh 2003-11-25
  • 打赏
  • 举报
回复
关键是用split函数按"|"符号把该string分成数组,楼上说了
rainstormmaster 2003-11-25
  • 打赏
  • 举报
回复
'利用词典实现
'首先加入对 Microsoft Scripting Runtime的引用
'窗体上一个按钮
Private Sub Command1_Click()
Dim s As String
Dim D1 As New Dictionary
Dim D2 As New Dictionary
s = "1:G101:20|1:G02:12|1:G103:11|2:G104:19|2:G106:24|3:G105:43"
Dim groups As Variant
Dim arr As Variant
arr = Split(s, "|")
Dim i As Long
Dim temp As Variant
For i = 0 To UBound(arr)
If arr(i) <> "" Then
temp = Split(arr(i), ":")
If UBound(temp) = 2 Then
If D1.Exists(temp(0)) = False Then
D1.Add temp(0), temp(1)
D2.Add temp(0), CLng(temp(2))
Else
D1.Item(temp(0)) = D1.Item(temp(0)) + "," + temp(1)
D2.Item(temp(0)) = D2.Item(temp(0)) + CLng(temp(2))
End If
End If
End If
Next
Dim j As Long
j = D1.Count
If j < 1 Then
Exit Sub
End If
groups = D1.Keys
Dim tmps As String
For i = 0 To j - 1
tmps = groups(i)
tmps = tmps + ":" + D1.Item(tmps) + ":" + CStr(D2.Item(tmps))
groups(i) = tmps
Debug.Print groups(i)
Next
Dim outs As String
outs = Join(groups, "|")
MsgBox outs
Set D1 = Nothing
Set D2 = Nothing
End Sub
northwolves 2003-11-25
  • 打赏
  • 举报
回复
Dim dazu As New Collection
Dim a() As String, b() As Long, c() As String
temp = Split(x, "|")
For i = 0 To UBound(temp)
dazu.Add Split(temp(i), ":")(0), "group" & Split(temp(i), ":")(0)
Next
ReDim a(dazu.Count - 1), b(dazu.Count - 1), c(dazu.Count - 1)
For j = 0 To dazu.Count - 1
For i = 0 To UBound(temp)

If Split(temp(i), ":")(0) = dazu(j + 1) Then
a(j) = a(j) & "," & Split(temp(i), ":")(1)

b(j) = b(j) + Val(Split(temp(i), ":")(2))
End If
Next
If Left(a(j), 1) = "," Then a(j) = Right(a(j), Len(a(j)) - 1)

c(j) = dazu(j + 1) & ";" & a(j) & ":" & b(j)
Next
Set dazu = Nothing
Erase a
Erase b
MsgBox Join(c, "|")
End Sub
Function fenxi(ByVal group As String) As String
northwolves 2003-11-25
  • 打赏
  • 举报
回复
刚才网页显示不全,下面代码没问题:
Private Sub Command1_Click()
On Error Resume Next
Dim x As String
x = "1:G101:20|1:G02:12|1:G103:11|2:G104:19|2:G106:24|3:G105:43|4:G106:15|5:G103:24|3:G101:41"
Dim dazu As New Collection
Dim a() As String, b() As Long, c() As String
temp = Split(x, "|")
For i = 0 To UBound(temp)
dazu.Add Split(temp(i), ":")(0), "group" & Split(temp(i), ":")(0)
Next
ReDim a(dazu.Count - 1), b(dazu.Count - 1), c(dazu.Count - 1)
For j = 0 To dazu.Count - 1
For i = 0 To UBound(temp)

If Split(temp(i), ":")(0) = dazu(j + 1) Then
a(j) = a(j) & "," & Split(temp(i), ":")(1)

b(j) = b(j) + Val(Split(temp(i), ":")(2))
End If
Next
If Left(a(j), 1) = "," Then a(j) = Right(a(j), Len(a(j)) - 1)

c(j) = dazu(j + 1) & ";" & a(j) & ":" & b(j)
Next
Set dazu = Nothing
Erase a
Erase b
MsgBox Join(c, "|")
End Sub
northwolves 2003-11-25
  • 打赏
  • 举报
回复
没讲清楚。
处理可用SPLIT函数,如:
Dim x As String
x = "1:G101:20|1:G02:12|1:G103:11|2:G104:19|2:G106:24|3:G105:43"
MsgBox Split(Split(x, "|")(2), ":")(1)

7,759

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧