I need to generate an array checking some conditions. Here is the scenario.
I have 3 variables containing strings like this:
client1 = "Google"
client2 = "Apple"
client3 = "Microsoft"
category1 = "sales"
category2 = "software"
category3 = "hardware"
The output i need to generate is an array which should have all the clients and the categories appended together by an underscore "_".
Desired Output: array = ["Google_sales", "Apple_software", "Microsoft_hardware"]
What I have tried so far:
array = [client1+"_"+category1, client2+"_"+category2, client3+"_"+category3]
Now, this works fine and I get what I want. But the complexity starts when a variable is empty. Consider there is another variable called client4="" and category4="". Now these are empty and when i try to complete my array i get messy array values.
Ex: array = [client1+"_"+category4, client4+"_"+category2]
This would give me an output like this: array = ["Google_", "_software"]
Question:
The user fills in the clients and the category of the clients. There might be a chance where the user failed to enter client or a category. Currently I have client1,client2,client3,client4 and cat1,cat2,cat3,cat4. Client1 is associated with cat1 and so on. Now I need to get an array only with the valid entries and if one of them is empty then we skip to the next one.
So we insert "_" in between client1 and cat1 only if both are present. Else we move to client2 and cat2 and so on.