How To Use Switch Statement to check if Values from Excel Column belongs to one Liste?

Dears,

How To Use Switch Statement to check if a string belongs to one List ?

Example :
String can be cell from one column
Liste 1 = {“A”,“B,“C”,“C”,“D”}
Liste 2 ={“Alpha”, “Beta”, Gama”, “Theta”}
Liste 3 ={“Item1”, “Item2”,“Item3”}


Liste 10 =

Note: I’m using ForEach Activity, and due to the fact that I have many lists ( In my project is 10), so it’s somewhat tedious , that’s why I’m thinking to use Switch Statmenent, and in case there is better solution it will be Good. Thx

WE would use a list of list and will Filter for the containing list or lists.

1 Like

Hi @hsendel

The Switch activity does not really allow you to work that way, nevertheless, you can “trick” it with an if nested inside the condition, and make it work as you want!

What you want to do is the following expression, but a bit more complex:

if(Condition,ValueIfTrue,ValueIfFalse)
  • You can use this expression inside the Switch condition, and assign values like numbers to “ValueIfTrue” or “ValueIfFalse” to reach your goal:

1- Define Arrays
image

2- Switch with Int32 Type (in my example, you can do it in a different way if you want) with the following condition (example for 3 contains, can be expanded)

If(var_Liste1.Contains(var_InputString),1,
	If(var_Liste2.Contains(var_InputString),2,
		If(var_Liste3.Contains(var_InputString),3,99
		)
	)
)
  • The result of this condition is a 1 if the inputString is inside List1, a 2 if the string is inside Liste2, and lastly a 3 if the string is inside the Liste3:

image

Note: The “99” at the end, is to have an option, in case our input string is not inside any Case.

You can see the relation below:

image

Example code:
ExampleSwitchContains.zip (56.6 KB)

Hope it helps!
Ignasi

2 Likes

Thanks a lot @ignasi.peiris , Probably I didn’t explain well what is the input and what should be the output.
Let me ask differently :
Keeping all above Assigned variables/Arrays, Suppose I have string " Alpha Bank" as input :
tugna

How to check that this string has SubString, which is “Alpha” belonging to var_Liste2 ? It’s Like spliting inputs string to substring and check if at least one of them belongs to one of the list :slight_smile:

Note : In case this will become more complicated, than I will keep existing workflow using ForEach Activity.

Thanks in advance

Got your point here!

With substring you mean for example, splitting by space? For example, on your input, you would check “Alpha” and “Bank”?

1 Like

Exact!!! and after spliting I don’t care about “Bank” as it doesn’t belong to all my lists.

Desired Output → One of the Subsrting of my Input String belongs to List “X”
Litle bit Similar to Topic : Find Substring of a String in Another String

Hello @ppr , Can this Kind of issues be solved by LINQuery?

You then can do the following (to follow a similar structure)

1- Get all substrings, like on the example, you can use " " as delimiter:

Split(var_InputString," ")

This should output: {“Alpha”,“Bank”}

Then you can use a For Each SubString (For Each - String Type) to loop them, and check with LinQ if they’re present in any of the previous arrays (I did as @ppr suggested, merge them into an Array of Arrays to make it easier)

var_ArrayOfArrays = {var_Liste1,var_Liste2,var_Liste3}

Then just a simple LinQ to check them:

  • Optional: I’m using a IF Condition to see if there is any match, and if it is, I print the array:
var_ArrayOfArrays.where(function(x) x.Contains(substring)).Any

If there is any match (Result = True) then you can print its value:

"SubString "+ substring+" found in array: "+String.Join(",",var_ArrayOfArrays.where(function(x) x.Contains(substring))(0))


image

Hope this helps now :slight_smile:

Re-Attaching example again (updated) → Substrings.xaml
ExampleSwitchContains.zip (59.1 KB)

1 Like

Awesome :+1: …Last Thing : Can we get var_Liste2 instead of array: Alpha,Beta,Gamma,Theta ?

Lets assume the requirement are now more fixed and we can use the lists/arrays

So first step would be to focus on how the match should be implemented e.g.
grafik

then the second step would be to define on how to handle full/partial matches of the searchString.

As mentioned, we can do it with a list of lists, like:

So we can do:

And we will get all the lists, where a (Partial)-Match is found

1 Like

Thanks @ppr , I will check and too and let you know.

The new Else If activity eliminates the need for this. It’s like a cross between an If and a Switch.

Just use an Else If activity. You’re all overcomplicating this.

Forget Switch. Use Else If - it overcomes the fact that you can’t use expressions in Switch cases.

1 Like

Hi @postwick , In fact both @ignasi.peiris & @ppr , gives an alternative ideas for this to avoid using Else IF, still only some adjustment to achieve this. Let’s see if they can do that or I will Keep using Else IF Statement.

Hello @ppr , Could you please share workflow for your example? Thx

kindly note: we are using only 1 Activity:
grafik

find starter help here:
CheckMatch_PartialString_inMultipleLists.xaml (5.7 KB)

1 Like

Thanks @ppr , from here I can only have the total items from different lists ( Same approach as @ignasi.peiris ), Is it possible to know in which List this PartialString belongs to ?

Hi @hsendel!

Maybe it would be easier to have a mapping table, for example, each Array/List of strings starting with its name.

Example:
Liste1 = {“Liste1”,“A”,“B”,“C”,“D”}
Liste2 = {“Liste2”,“Alpha”,“Beta”,“Gamma”,“Theta”}
etc…

Then when found, just print the FoundVariable(0).ToString.

Another approach you can do is using Invoke Code (C#mode, couldn’t make it work in VB.Net, I believe it’s not supported yet) and use “NameOf” Expression:

Link: NameOf Expression

Code:

Console.WriteLine(nameof(var_Liste));

image

You need somehow to make your way and change the “var_Liste” from the snippet above, and it will print the name, but I don’t think it’d be the best option based on what we have now.

If you can modify the Lists / Arrays, you can try and go with the 1st option, otherwise, you can try the 2nd one.

Hope it helps!
Ignasi

1 Like