How to use a dictionary properly for my case

I have a DataTable like following:
[Qty] [ItemName]
[ 1 ]… [Tomato Juice]
[ 1 ]… [Juicy Pear Juice]
[ 2 ]… [Golden Apple Juice]
[ ] … [with Honey]
[ 1 ] … [Grape Juice]

What I need to do is, get the item name as the Dictionary key, and get the Qty as its value.
What’s making this difficult for me is, as you can see, “Golden Apple Juice with Honey” is one string taking up 2 rows. You can tell the previous item name is taking up the current row by looking at the qty of the current row. If the current row is empty, you can assume that the item name needs to be concatenated from the previous one.

The result should look something like:
<key, value>dict[0] = <“Tomato Juice”, 1>
<key, value>dict[1] = <“Juicy Pear Juice”, 1>
<key, value>dict[2] = <“Golden Apple Juice with Honey”, 2>
<key, value>dict[3] = <“Grape Juice”, 1>

How could I do this? By the way, the item name can take up more than 2 rows. It can be 3 rows, 4 rows etc. The only way to know is that the Qty value will be empty for those rows.

Hi,

Hope the following sample helps you.

Sample20210331-1.zip (3.3 KB)

Regards,

1 Like

Hi @Raj222
Sharing another approach just in case.

Main.xaml (12.7 KB)

P.S. Also in a dictionary your keys need to be unique which is not the case here. It might cause some data loss for you.

1 Like

I think that your solution is nice but the key should be row(1).toString and value is Qty row. Qty is null or empty then it should use this previous key edit option :wink:

@Yameso Yeah, great fix to make the solution complete. Thanks!!!

After checking once again I dont think your solution would give the right result. This solution do not remove keys, which has to be changed. Also it not includes situation where there is more than 2 rows to concatenate.

@Raj222 I think you should use @Yoichi solution :wink:

Yoichi created dicitonary(string,int32) but you can also change this solution and use (string,string). This depends on your needs :slight_smile:

It actually does account for the situation when more than 2 rows are to be added. See, str_previous only updates in the else section of if condn. In the if section if we encounter a white space or null value the initial str_previous(which was same for 1st and 2nd row) will continue for the third row and so on. I specifically coded this keeping that in mind.
image
image
Here clearly the last three rows are being concatenated.

Also I don’t understand, in which condns do we need to remove keys??

1 Like

You created dict where keys are those qty number. This should be created reversed.

If first key is golden it has value 2. Next is empty cell so it shoud be Key “Golden Pear” with value 2. But we dont need “Golden” key anymore so thats why its removed and instead we add “Golden Pear” with value 2. Next row is also empty so robot need to remove “Golden Pear” and add “Golden Pear Is cool” with the same value (2).

In that scenario we need to edit keys, not values. I dont know other way for edting key in dict then removing old one and adding edited key with the same value.

@Yoichi did exactly that.

2 Likes

Thank you all! and yes, I am using @Yoichi’s solution for now and it is working great.
I was exactly thinking that maybe I should add a new key-value pair to the dictionary object regardless as long as a row exists, and then if the QTY is empty, delete the one that was just added, and add a new key, which is a concatenated string from the previous key.

I just didn’t know how to nicely implement this, and when I tried, my code was getting messier and messier, and at some point I had to ask for help on here.

1 Like

Mark then Yoichi post as solution :slight_smile:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.