VBScript: 새로운 분할 항목이 있는 리조트 문자열 및 그룹 (VBScript: Resort String and group with new Split Item)


문제 설명

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

새 설명:*

  1. 기본 문자열을 "#"으로 분할합니다.

  2. 분할된 각 부분에서... "$"와 이름을 "groupId"로 지정합니다. (그룹화 및 정렬을 위한 중요한 매개변수입니다.)

  3. 각 부분은 모두 동일한 groupId를 가지며 나란히 배치되어야 하며 "로 결합되어야 합니다. #".

  4. 위의 단계 후에... 우리는 "@"로 다른 groupId와 모든 새 문자열을 결합해야 합니다....와 동일합니다. (00$01,05 #01,06#...@02,07@03,4.....)

06#...@02,07@03,4.....)

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 sadrasjdJohn Coleman)

참조 문서

  1. VBScript: Resort String and group with new Split Item (CC BY‑SA 2.5/3.0/4.0)

#split #grouping #vbscript






관련 질문

오류: 이 범위에서 '분할'이 선언되지 않았습니다. (error: ‘split’ was not declared in this scope)

화살표 문자가 포함된 문자열 분할 (Split a string containing arrow characters)

문자 수로 문자열의 특정 부분 가져오기 (Take specific part of a string by character count)

VBScript: 새로운 분할 항목이 있는 리조트 문자열 및 그룹 (VBScript: Resort String and group with new Split Item)

python 태그에서 단어와 숫자 나누기 (Split words and numbers from tags python)

구두점을 문자열 끝에서 시작 부분으로 이동하려면 어떻게 해야 합니까? (How can I move the punctuation from the end of a string to the beginning?)

쉼표로 문자열을 분할하지만 대괄호 또는 따옴표로 묶인 쉼표는 무시하십시오. (Split string by comma but ignore commas in brackets or in quotes)

숫자를 나누고 점 뒤에 하나의 숫자를 유지하십시오. (Split the number and keep one number after the dot)

Oracle에서 쉼표로 구분된 문자열의 최대 개수를 얻는 방법은 무엇입니까? (How to get maximum COUNT of comma separated string in Oracle?)

분할 방법을 사용하여 텍스트 파일에서 범주를 열로 분리 (Using the split method to separate categories into columns from a text file)

Powershell에서 문자열 분할 및 추가 (Splitting and Adding String in Powershell)

원래 문자열 포인터를 수정하지 않는 strtok_r() 및 strsep()에 대한 C-문자열 대안? (C-string alternatives to strtok_r() and strsep() that don't modify the original string pointer?)







코멘트