I have a strint that i want build a dictionary variable.
Sample:
Item1 : Result1
Item2 : Result2
Item3 : Result3
…and so on
This is basicaly the structure from it. I want to build the dictionary variable to display the result based on the selected item.
Like: DictionaryVariable(“Item2”).ToString
Result: Result2
I don’t know how to input the string into the dictionary and use the colon to split the key and value.
ppr
(Peter Preuss)
August 2, 2022, 6:17pm
2
MaysonLedur:
Item1 : Result1
maybe you are looking for this:
Create a variable: dictItems
assign Activity
dictItems = new Dictionary(Of String, String)
add following string Item1 : Result1 - str1
Assign Activity:
dictItems(str1.Split(":“c)(0).Trim) = str1.Split(”:"c)(1).Trim
Thats no good unfortunately. This way, i would have to do the split on every row of my string, and the row number is variable too. I need to input the entire string at once.
ppr
(Peter Preuss)
August 2, 2022, 6:32pm
4
then it would be better that you are describing your case more exact
unlcear what is meant in detail
just give us some sample strings and expected resulting dictionary
ushu
(Usha kiranmai)
August 2, 2022, 6:38pm
5
@MaysonLedur How about this
Refer below workflow
Example.zip (2.8 KB)
1 Like
ppr
(Peter Preuss)
August 2, 2022, 6:50pm
6
In general we would more focus on a strategy like:
bring the values into a collection - e.g. Array, List…
create the dictionary from this (e.g. LINQ, Loops)
enrich / Split / generate the Keys
Example:
where in your case it could also be like:
arrItems = {strVar1, strVar2 …}
arrItems.Select(Function (x) x.Split(":"c).Select(function (s) s.Trim)).ToDictionary(Function (a) a(0), Function (a) a(1))
My imput string could be like:
Item1 : Result1
Item2 : Result2
Item3 : Result3
or
Item4 : Ball
Item5 : 12345
Item6 : Car
Item7 : I’m lost at this
The only constancy is a String on left, " : " in between and another String on Right
ppr
(Peter Preuss)
August 2, 2022, 7:05pm
8
most of the working working blocks are demonstrated as above
here it can be more exact.
But lets assume you string is e.g. content from a txt file / get text… and has line breaks
then the same as described above
just to mention one of several options
1 Like
jeevith
(Jeevith Hegde, Ph.D.)
August 2, 2022, 7:43pm
9
Hi @MaysonLedur ,
As an alternative to the approach @ppr suggested.
The below approach will also take care of both your fixed pattern and is independent of the number of rows in your string.
We split at new line, the output array consist of fixed patterns (left:right) which we can extract by another split as shown.
Finally the add method adds the key values to a output dictionary.
Here is the .xaml file : StringToDictionary.xaml (11.1 KB)
Hope this helps.
1 Like
Here is my soluction.
strIntoDictionary.xaml (10.0 KB)
1st: Convert the string into an array using “new line” as separator
2nd: Declair the Dictionary variable
3rd: For Each item on the Array and “Add to Dictionary” activitie (from Microsoft.Activities.Extensions) where the
Key: Split(item," : “)(0) and Value: Split(item,” : ")(1)
Hope i can help someone else!
jeevith
(Jeevith Hegde, Ph.D.)
August 2, 2022, 8:09pm
11
You can also do away with installation of additional libraries if you use the Invoke Method
activity (as shown above).
1 Like
system
(system)
Closed
August 5, 2022, 8:13pm
13
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.