Copying from one list to another

Hi,

I am a new learner. I came up with a simple task and I cant solve it.
I have a list of some numbers: (Ai123, Ai243, Bi344, Bi3443, Ci24244, Ci48484). Lets say that these numbers are some kind of invoice numbers. And I would like to solve my issue with “If and for each”.
I would like you to sort these invoices from my list into 3 separate lists (starting with Ai, Bi and Ci) and add each invoice accordingly to one of the three new lists.
I started by creating an “InvoiceList” with assign: InvoiceList = New list (of String) From {“Ai123”, “Ai243”, “Bi344”, “Bi3443”, “Ci24244”, “Ci48484”} and then created 3 empty lists with assign: AInvoiceList, BinvoiceList and CinvoiceList
image

Then I create a “for each loop” to loop through each item (invoice) in the InvoiceList.
In the body of the loop I put IF “item.ToString.StartsWith (” Ai “)”
For “then” I added in the sequence “Add to Collection” and there it adds each item that meets the condition to the AInvoiceList. In “Else” I threw a second IF that checks if item.ToString.StartsWith (“Bi”) if so, then “Add to Collection” and adds an item that meets the BinvoiceList condition, and in “Else” - “Add to Collection” and items are flying to CinvoiceList


This is how the example properties for “Add collection” look like:
image

And I think that it does not work. what am I doing wrong? What is the way to write all items from the list?

Thanks in advance for all suggestions and explanation.

When you say it doesn’t work, what do you mean?

Also, there is an Else If activity that works better than nested Ifs.

I think everything is correct, are you getting any errors while running, please share those error log messages,
one more thing in the assign use “New List(Of String)” in all the statements, I saw in some you have written “New list (of String)” it may give errors(validation errors) in some situations

maybe a switch where the first Letter will be used for the splitting can serve better.

Have a check on it

Also there is a simpler way to do this. You don’t even need the Ifs. In your Add to Collection activity, where you tell it the collection to add to, just do:

left(item,2) + “Collection”

This will give you AiCollection, BiCollection etc depending upon the first two letters of the item.

Maybe it works but I have no idea how to write down all three lists to see them in my output. Moreover in my output I have letter “y” and I dont know why.
image

Regarding “y” It was my mistake. I have an additonal Log Message.

Use Debug instead of Run. Then right-click your activities and set breakpoints. It’ll pause at each breakpoint and then you can look at the debug panels on the left to see the realtime values of each variable/list.

1 Like

hey

check this. Made an example for your understanding

test.xaml (10.9 KB)

regards!

1 Like

Thank you very much for your help. Now I understand it :slight_smile:

if it helps remember to close the topic,

regards!

Hi @Igor_Wolny ,

Here is another way to approach the problem.
I hope it gets you thinking in creative ways →

lst_masterSet = New List(Of String) From {"Ai123", "Ai243", "Bi344", "Bi3443", "Ci24244", "Ci48484"}
lst_searchCriterias = lst_masterSet.Select(Function(s) s.Substring(0,2)).Distinct().ToList()
dict_items = lst_searchCriterias.ToDictionary(Function(k) k, Function(v) lst_masterSet.Where(Function(w) w.StartsWith(v)).ToList())

ListExercise.xaml (5.9 KB)

Kind Regards,
Ashwin A.K

2 Likes

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