@hsendel posted a new topic, subject "How To Use Switch Statement to check if Values from Excel Column belongs to one Liste?". It read:

   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

Then @ppr (Peter) said

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

After which, @ignasi.peiris (Ignasi Peiris) added

    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:
    [code] 
        if(Condition,ValueIfTrue,ValueIfFalse)
        
    [/code]
    
      * 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  
    
    
    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)
    [code] 
        If(var_Liste1.Contains(var_InputString),1,
        	If(var_Liste2.Contains(var_InputString),2,
        		If(var_Liste3.Contains(var_InputString),3,99
        		)
        	)
        )
        
    [/code]
    
      * 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:
    
    
    
    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:
    
    Example code:  
    ExampleSwitchContains.zip (56.6 KB)
    
    Hope it helps!  
    Ignasi

Then @hsendel said

    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 :  
    
    
    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 
    
    Note : In case this will become more complicated, than I will keep existing workflow using ForEach Activity.
    
    Thanks in advance

After which, @ignasi.peiris (Ignasi Peiris) added

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

After which, @hsendel added

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

Then @hsendel said

    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

After which, @hsendel added

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

Then @ignasi.peiris (Ignasi Peiris) said

    You then can do the following (to follow a similar structure)
    
    1- Get all substrings, like on the example, you can use " " as delimiter:
    [code] 
        Split(var_InputString," ")
        
    [/code]
    
    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)
    [code] 
        var_ArrayOfArrays = {var_Liste1,var_Liste2,var_Liste3}
        
    [/code]
    
    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:
    
    
    [code] 
        var_ArrayOfArrays.where(function(x) x.Contains(substring)).Any
        
    [/code]
    
    If there is any match (Result = True) then you can print its value:
    [code] 
        "SubString "+ substring+" found in array: "+String.Join(",",var_ArrayOfArrays.where(function(x) x.Contains(substring))(0))
        
    [/code]
    
    image576×793 25.4 KB
    
      
    
    
    Hope this helps now 
    
    Re-Attaching example again (updated) → Substrings.xaml  
    ExampleSwitchContains.zip (59.1 KB)

To which @hsendel replied

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

After which, @ppr (Peter) added

    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.  
    
    
    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:  
    
    
    grafik784×163 11.7 KB
    
    So we can do:  
    
    
    grafik707×137 4.34 KB
    
    And we will get all the lists, where a (Partial)-Match is found

@hsendel replied

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

Then @postwick (Paul) said

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

So @postwick (Paul) replied

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

After which, @postwick (Paul) added

    Forget Switch. Use Else If - it overcomes the fact that you can't use expressions in Switch cases.
    
    UiPath Activities
    
    ### Else If
    
    UiPath.Core.Activities.IfElseIf Models an If-Then-Else If-Else condition. The activity has three main initial sections: a condition, a body and an Add Else or If Else button. The activities included in the body will not run if the condition specified...

So @hsendel replied

    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.

After which, @hsendel added

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

@ppr (Peter) replied

    kindly note: we are using only 1 Activity:  
    
    
    find starter help here:  
    CheckMatch_PartialString_inMultipleLists.xaml (5.7 KB)

@hsendel replied

    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 ?

So @ignasi.peiris (Ignasi Peiris) replied

    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:
    [code] 
        Console.WriteLine(nameof(var_Liste));
        
    [/code]
    
    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

After which, @ppr (Peter) added

    Have a look on following approach:  
    
    
    grafik932×53 4.26 KB
    
    Using a dictionary with its key for the different lists
    
    grafik743×311 17.4 KB
    [code] 
         dictLists
         Dictionary<string, List<string>>(3) { { "Letters", List<string>(5) { "A", "B", "C", "C", "D" } }, { "Greek", List<string>(4) { "Alpha", "Beta", "Gama", "Theta" } }, { "Items", List<string>(3) { "Item1", "Item2", "Item3" } } }
         strCheck
         "Alpha Bank"
         arrMatchKeys = dictLists.Keys.Where(Function (x) dictLists(x).Intersect(strCheck.Split(" "c)).Count > 0).toArray
         string[1] { "Greek" }
         arrMatchKeys
         string[1] { "Greek" }
        
    [/code]
    
    for sure, The LINQ statement can also be decomposed to essential activities. The approach will work dynamicly over all contained lists within the dictionary and requires no change e.g. when additional lists are added to the dictionary.

To which @system (system) replied

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


{'source': 'discourse', 'url': 'https://forum.uipath.com/t/how-to-use-switch-statement-to-check-if-values-from-excel-column-belongs-to-one-liste/389434', 'title': 'How To Use Switch Statement to check if Values from Excel Column belongs to one Liste?', 'views': 1206, 'has_accepted_answer': True, 'word_count': 1260, 'like_count': 10, 'reply_count': 16, 'participant_count': 4, 'posts': 22, 'tags': '', 'author': '@hsendel', 'participants': '@hsendel, @ppr (Peter), @ignasi.peiris (Ignasi Peiris), @postwick (Paul), @system (system)', 'image_url': [None], 'created_at': '2022-01-07T19:46:08.285Z', 'last_posted_at': '2022-01-09T16:22:50.927Z'}