-1

I'm trying to find a better solution for the integration of a string and generate a new field with the maximum value of the parameter. @AutomatedChaos has helped me with the following code. But I need a better solution for the flexibility of the code.

First string split by * (stars) and I want to merge all items and create a new string with max value.

fString = "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(fString)
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

I want to generate this string:

newString = "projects@dnProjectsPatterning=5|dnProjectsSendReport=3#workplans@dnWorkplansAdd=1|dnWorkplansGrouping=2"

Please note for projects@ and workplans@, the two are split by #.

sadrasjd
  • 61
  • 10
  • According to [this post](http://stackoverflow.com/a/13610649/1270789), `"projects@" & String.Join("|", Me.Select(Function(dict) String.Format("{0}={1}", dict.Key, dict.Value).ToArray()))`, but I don't know if that works in VBScript as well as in VB.NET. – Ken Y-N May 09 '17 at 02:08
  • "projects@", "workplans@" must be splited with "#".. and shoud be dinamically... tanx – sadrasjd May 09 '17 at 02:18
  • @KenY-N no the syntax is different, the equivalent function in VBScript is `Join()` it accepts `Array, Delimiter` arguments. – user692942 May 09 '17 at 10:26

2 Answers2

3

Here's another example that will work if the parameters are always ordered as shown.

The following code simply treats all separators except * as part of the keys. You can look at this regexr shot to see how the pattern works.

fString = "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 params = CreateObject("Scripting.Dictionary")
With (New RegExp)
    .Global = True
    .Pattern = "([^=*]*)=(\d+)"
    For Each match In .Execute(fString)
        key = match.Submatches(0)
        val = match.Submatches(1)
        If params.Exists(key) Then
            If val > params(key) Then params(key) = val
        Else
            params.Add key, val
        End If
    Next
End With

'temporary str dictionary to generate string
Set str = CreateObject("Scripting.Dictionary")
For Each key In params
    'prepend key + "=" into items to generate merged string
    str.Add key, key & "=" & params(key)
Next
newString = Join(str.Items, "") 'joining items
WScript.Echo newString

'normalize params' keys
For Each key In params
    If Left(key, 1) = "|" Or Left(key, 1) = "#" Then
        params.Key(key) = Mid(key, 2)
    End If
Next

'lookup for `dnProjectsSendReport` parameter

WScript.Echo params("dnProjectsSendReport") 'must print 3
Kul-Tigin
  • 16,728
  • 1
  • 35
  • 64
0

I find a solution:

'target = "projects@param1={param1}|param2={param2}#workplans@param3={param3}|param4={param4}..."
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,"*")
endString = Split(arrAccessPack(0),"#")
nArrString = ""
for i = 0 to UBound(endString)
    if i < UBound(endString) then strHash = "#" else strHash = ""
    part1 = Split(endString(i),"@")(0)
    part2 = Split(Split(endString(i),"@")(1),"|")
    newParams = ""
    for j = 0 to UBound(part2)
        if j < UBound(part2) then strPipe = "|" else strPipe = ""
        param = Split(part2(j),"=")(0)
        newParams = newParams & param&"={"&param&"}" & strPipe
    next
    nArrString = nArrString & part1&"@"&newParams & strHash
next
MergeAccessArray = MergerParams(f_AccessArray,nArrString)


Function MergerParams(fStr,fTarget)

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

Set matches = re.Execute(fStr)
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

target = fTarget
for each key in dict
    target = Replace(target, "{" & key & "}", dict.Item(key))
Next
MergerParams = target

End Function
sadrasjd
  • 61
  • 10