Hello Team,

I am having a requirement to split string of (Key, Value) format. First value will be field name and second value will be its value. Input string may vary with different length.
Example :
Input String : 'Name|Naveen|Job|Software|Country|India'.
Expected Output : Name = Naveen
Job = Software
Country = India
values to be in separate data item or in collection.

I wrote custom code to achieve this requirement, but it is throwing an error. Please find attached screenshot for error.

My Code :

char[] delimiterChar = {'|'};
string input = "Name|Naveen|Job|Software|Country|India";
Dictionary<string,string> output= VisitorDetail(input.Trim('|'));
Dictionary<string,string> VisitorDetail(string input)
{
Dictionary<string, string> result = new Dictionary<string, string>();
string[] keyValues = input.Split(delimiterChar);
for (int i = 0; i < keyValues.Length; i = i+2)
{
result.Add(keyValues, keyValues[i + 1]);
}
return result;
}

'result' am storing in collection.

Is there any other way to achieve this requirement?

Thanks.
 

Attachments

  • Key Value Custom code.png
    33.1 KB · Views: 136

VJR

Well-Known Member
Staff member
Hi Naveen Pittala,

If you are struggling to work with the code for splitting the delimiter character then you can at first use the 'Split Text' action via Blue Prism which will return a collection and then work with your loop on that collection (Data table).
 

VJR

Well-Known Member
Staff member
Check the attached xml file which would be much easier rather than explaining with screenshots.
Although this works fine with your above mentioned input string, this is not a fully tested version so you need to run the tests on various scenarios of your input data.

The Multi Calc stage is to find the remainder since I didn't find a direct Remainder operator after division between two numbers.
The odd and even number check is used to identify when it is time in the loop to have a string like Key = Value, keeping in mind that the collection index starts from 0.

Let me know your thoughts on the process after running through various inputs.

Process Diagram:
View attachment 1542174795435.png

Input:
View attachment 1542174308146.png

Output:
View attachment 1542174273617.png
 

Attachments

  • BPA Process - Split pipe and make equal to.zip
    2.4 KB · Views: 245
@VJR Thanks for your help.

Above logic really helped for me.

I am having one more small requirement in this.

How can I make 'Name, Job, Country' as column headers in the collection and 'Naveen, Software, India' as column values.

Please find attached screenshot for collection data.
 

Attachments

  • Column values as Headers.png
    17.1 KB · Views: 169
  • Expected Collection.png
    4.8 KB · Views: 148

VJR

Well-Known Member
Staff member
Hi Naveen Pittala,

Is your collection defined or undefined? Meaning - are you going to be aware beforehand that your final collection is going to have Name, Job and Country as the three columns OR it can be any columns coming from the earlier collection?

I'll provide the solutions for both and depending on what it is, you can use the desired approach.
If its a defined collection, then you need to create a collection in your diagram having these three columns. Then by using a MultiCalc stage you can set the values of these three columns from the 'Value' column of your first screenshot.

I am pretty sure it is going to be an undefined collection. This is going to be tricky so you will need to use a back door entry approach.
- Write the Collection in your first screenshot in an Excel (which can be deleted later)
- Use the 'Select' action to select the data on the Excel. You can do a dynamic selection using the 'Get Number of Rows' action.
- Use the 'Copy' action
- Then 'Select' a new range. This is the destination range where you want the data to be pasted. You need not give a full Range, only "A1" is sufficient. Give it in some far away area of the sheet or even in another sheet.
- Now you need to do a Paste as Transpose of the copied data. This can be be done by doing a minor change in a new action as shown in Post #2 here.
- This will paste the data in a Transposed format as below
View attachment 1542252829573.png

The upper portion is the original written collection and the bottom one is the Paste as Transpose using the new action that I had created earlier for the other post.

- Now use the 'Get Worksheet Range As Collection' by giving the range of the bottom portion or your new sheet
- Doing so gives the output in a collection as follows.
View attachment 1542253160757.png

If you would like to go with an approach other than Excel then you might need to write a custom code on the collection.
 

VJR

Well-Known Member
Staff member
@VJR Thanks for the help!!!

It worked for me.

If possible can we change this post as solved.
Hi Naveen,

Since you are the original poster of the thread you should be able to see an option/dropdown perhaps on the top right hand side to close the thread.
Do post back if you are unable to.
 

hari

New Member
Check the attached xml file which would be much easier rather than explaining with screenshots.
Although this works fine with your above mentioned input string, this is not a fully tested version so you need to run the tests on various scenarios of your input data.

The Multi Calc stage is to find the remainder since I didn't find a direct Remainder operator after division between two numbers.
The odd and even number check is used to identify when it is time in the loop to have a string like Key = Value, keeping in mind that the collection index starts from 0.

Let me know your thoughts on the process after running through various inputs.

Process Diagram:
View attachment 2514

Input:
View attachment 2513

Output:
View attachment 2512

Thank you it helped me
 
Top