문제 설명
VBScript: 새로운 분할 항목이 있는 리조트 문자열 및 그룹 (VBScript: Resort String and group with new Split Item)
'나는 "#"으로 분할된 아래 문자열이 있습니다.
myStr = "78,6$25,01|25,02|25,03|25,04#74,5$15,01|15,02|15,03|15,04#70,1$25,06|25,07|25,08|25,09#77,3$25,07|25,08|25,09|25,10#78,2$10,05|10,06|10,07|10,08"
'이 새 문자열에 새 그룹화를 사용하고 싶습니다...
'달러($) chrachter 다음의 첫 번째 값으로 분할된 문자열을 그룹화해야 합니다.
하지만 새로운 원하는 결과로 정렬 및 그룹화하는 방법을 모르겠습니다. :
myStrDesired = "78,2$10,05|10,06|10,07|10,08@74,5$15,01|15,02|15,03|15,04@78,6$25,01|25,02|25,03|25,04#70,1$25,06|25,07|25,08|25,09#77,3$25,07|25,08|25,09|25,10"
6내 스크립트:
Function GroupArrays()
myStr = "78,6$25,01|25,02|25,03|25,04#74,5$15,01|15,02|15,03|15,04#70,1$25,06|25,07|25,08|25,09#77,3$25,07|25,08|25,09|25,10#78,2$10,05|10,06|10,07|10,08"
'And I want resort to this New String with New Grouping...
'i should group splited string with first value after dollar($) chrachter
myStrDesired = "78,2$10,05|10,06|10,07|10,08@74,5$15,01|15,02|15,03|15,04@78,6$25,01|25,02|25,03|25,04#70,1$25,06|25,07|25,08|25,09#77,3$25,07|25,08|25,09|25,10"
arrMyStr = Split(myStr,"#")
arrMyStrDesired = ""
for i = 0 to UBound(arrMyStr)
' find group id from each string
groupVal = Split(Split(arrMyStr(i),"$")(1),",")(0)
' put the same groups together and split them by "#" And finally the isolation of other disciplines with "@"
arrMyStrDesired = arrMyStrDesired & arrMyStr(i)
next
GroupArrays = arrMyStrDesired
End Function
06#...@02,07@03,4.....) 06#...@02,07@03,4.....)새 설명:*
기본 문자열을 "#"으로 분할합니다.
분할된 각 부분에서... "$"와 이름을 "groupId"로 지정합니다. (그룹화 및 정렬을 위한 중요한 매개변수입니다.)
각 부분은 모두 동일한 groupId를 가지며 나란히 배치되어야 하며 "로 결합되어야 합니다. #".
위의 단계 후에... 우리는 "@"로 다른 groupId와 모든 새 문자열을 결합해야 합니다....와 동일합니다. (00$01,05 #01,06#...@02,07@03,4.....)
참조 솔루션
방법 1:
The following should work on any Windows machine with the Dot Net runtime. If for some reason you don't have that ‑‑ would need a custom sort:
myStr = "78,6$25,01|25,02|25,03|25,04#74,5$15,01|15,02|15,03|15,04#70,1$25,06|25,07|25,08|25,09#77,3$25,07|25,08|25,09|25,10#78,2$10,05|10,06|10,07|10,08"
myDesiredStr = "78,2$10,05|10,06|10,07|10,08@74,5$15,01|15,02|15,03|15,04@78,6$25,01|25,02|25,03|25,04#70,1$25,06|25,07|25,08|25,09#77,3$25,07|25,08|25,09|25,10"
Function GroupVal(group)
A = Split(group,"$")
B = Split(A(1),",")
GroupVal = CInt(B(0))
End Function
Function ReSort(str)
Set D = CreateObject("Scripting.Dictionary")
Set keyList = CreateObject("System.Collections.ArrayList")
groups = Split(str,"#")
For i = 0 to UBound(groups)
group = groups(i)
v = GroupVal(group)
If D.Exists(v) Then
D.Item(v) = D.Item(v) & "#" & group
Else
D.Add v,group
keyList.Add v
End If
Next
keyList.Sort()
newGroups = Array()
ReDim newGroups(Ubound(groups))
i = ‑1
For Each v In keyList
i = i + 1
newGroups(i) = D.item(v)
Next
ReDim Preserve newGroups(i)
Resort = Join(newGroups,"@")
End Function
MsgBox myDesiredStr = Resort(myStr)
The msgbox pops up True
(by sadrasjd、John Coleman)