-2

I have a string that splitted by * (stars) and I want to merge all items and create a new string with max value. How can get max value for new string?

Function MergeAccessArray()
  f_AccessArray = "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"

  arrAccessPack = Split(f_AccessArray, "*")
  nArrString = ""
  For i = 0 To UBound(arrAccessPack)
    If i < UBound(arrAccessPack) Then strPipe = "|" Else strPipe = ""
    arrParts = Split(arrAccessPack(i), "#")
    nArrPartString = ""
    For j = 0 To UBound(arrParts)
      'If j < UBound(arrParts) Then iPipe = "|" Else iPipe = ""
      nPart = Split(arrParts(j), "@")(1)
      nArrPartString = nArrPartString & nPart & strPipe
    Next
    nArrString = nArrString & nArrPartString & strPipe
  Next
  nArrString = Replace(nArrString, "||", "|")

  xArrString = Split(nArrString, "|")
  For x = 0 To UBound(xArrString)
  Next
  MergeAccessArray = nArrString
End Function

I want this String:

arrNewParameters = "projects@dnProjectsPatterning=5|dnProjectsSendReport=3#workplans@dnWorkplansAdd=1|dnWorkplansGrouping=2"
user692942
  • 16,398
  • 7
  • 76
  • 175
sadrasjd
  • 61
  • 10
  • 1
    For each parameter, add it to a `Scripting.Dictionary` object with the parametername as key and the value as value. When a parameter already exist, overwrite it with the max value. When you have processed all parameters, rebuild your string with the keys and values from the dictionary. – AutomatedChaos May 08 '17 at 08:02
  • tanx sir... I do not have experience of working with dictionaries.. and I think my codes are not optimal – sadrasjd May 08 '17 at 08:27

1 Answers1

1

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.

AutomatedChaos
  • 7,267
  • 2
  • 27
  • 47
  • Tanx AutomatedChaos... for your creative approach... can you say that reform in accordance with the following format? **"projects@dnProjectsPatterning=5|dnProjectsSendReport=3#workplans@dnWorkplansAdd=1|dnWorkplansGrouping=2"** nArr = "" for each key in dict nArr = nArr & key & "=" & dict.Item(key)&"|" Next msgbox nArr – sadrasjd May 08 '17 at 12:38
  • Can you help me for create this string: **"projects@dnProjectsPatterning=5|dnProjectsSendReport=3#work‌​plans@dnWorkplansAdd‌​=1|dnWorkplansGroupi‌​ng=2"** – sadrasjd May 08 '17 at 13:41
  • **I was troubled** plz help – sadrasjd May 08 '17 at 13:42
  • @sadrasjd see the edit – AutomatedChaos May 08 '17 at 14:02
  • Tanx for your help – sadrasjd May 09 '17 at 01:41