[Solved] Merge multi strings and create new string with set max parameters value [closed]


As a follow up to my comment, this is an example on how you can use the dictionary to get a list of unique keys and add the highest value to it:

s = "projects@dnProjectsPatterning=0|dnProjectsSendReport=1#workplans@dnWorkplansAdd=0|dnWorkplansGrouping=1*projects@dnProjectsPatterning=1|dnProjectsSendReport=3#workplans@dnWorkplansAdd=1|dnWorkplansGrouping=0*projects@dnProjectsPatterning=5|dnProjectsSendReport=1#workplans@dnWorkplansAdd=0|dnWorkplansGrouping=2"

Set dict = CreateObject("Scripting.Dictionary")
Set re = New RegExp
re.Global = True
re.Pattern = "(\w+)=(\d+)"

Set matches = re.Execute(s)
for each match in matches
    key = match.Submatches(0)
    value = cint(match.Submatches(1))
    if dict.Exists(key) Then
        if value < dict.Item(key) then
            value = dict.Item(key)
        End If
    End If
    dict.Item(key) = value 
next

for each key in dict
    msgbox key & "=" & dict.Item(key)
Next

' output:
' dnProjectsPatterning=5
' dnProjectsSendReport=3
' dnWorkplansAdd=1
' dnWorkplansGrouping=2

Edit: This is an example that will generate the string for you, but it will only do it for given keys.

target = "projects@dnProjectsPatterning={dnProjectsPatterning}|dnProjectsSendReport={dnProjectsSendReport}#dnWorkplansAdd@dnWorkplansAdd={dnWorkplansAdd}|dnWorkplansGrouping={dnWorkplansGrouping}"
for each key in dict
    target = Replace(target, "{" & key & "}", dict.Item(key))
Next
msgbox target

Tip: If you expect more and better answers from the SO community, start out with improving you questions and show the effort you already did while trying to get to a solution.

5

solved Merge multi strings and create new string with set max parameters value [closed]