Optimizing code for conditional "if" assignments

Is there a way to simplify this code? The system of boxes for a simple “if this condition is met, assign a value” seems overly cumbersome to me.

Hi,

How about using Regex.Replace?

newString = System.Text.RegularExpressions.Regex.Replace(yourString,"^INGRESOS TOTALSE$","Ingresos")

Regards,

I do not need to replace the values. It is a series of “if” statements nested within other “if” statements where the “else” part is not used. The code becomes very difficult to read.


Hi,

Because the above can assign to replaced string to other variable, it can replace IF activity.

Of it’s necessary to convert a lot of pairs of keyword, the following approach using dictionary will help you.

dict = New Dictionary(Of String,String)From{{"INGRESOS TOTALSE","Ingresos"},{"From","To"}}

Next check if it has input string (let’s say yourString) as key. Then we can use dict(yourString)

Regards,

Hello, to be honest, I did not understand your solution. I am attaching a pseudocode to clarify what I need. The number of ‘if’ statements makes the workflow very difficult to read, as the IF activity does not allow the removal of the ‘else’, nor does it permit the inclusion of two conditions within it.

for x= 1 to 20
if cell = “Ingresos” then
Flag = 1
end if
if cell = Total" then
if flag= 1 then
value= “X”
flag = 2
end if
if flag = 2 then
value = “Y”
end if
end if
next

@Matias_Clemente.Arg

is this what you are looking for…to make it less clutter

this is else if activity

cheers

Hello @Matias_Clemente.Arg

Perhaps you could use a Switch activity to make the design canvas more sleek to look at, in some of your cases.

Assuming that some of the cases could only have one outcome, the switch can have several cases, but only select one of them.

Switch expressions = salida.ToString
Case "INGRESOS TOTALES"
Do something...
Case "EGRESOS TOTALES"
Do something else...

Regards
Soren

@Anil_G , please correct me if I am wrong…

My understanding is that, in an ElseIf activity, if a condition evaluates true, all other conditions will be skipped for that iteration, which means flag=1 and flag=2 will never be true?

I haven’t actually tested my theory though :grinning:

@Matias_Clemente.Arg ,

From a quick look, with the way you’ve structured the logic, flag will never be 1 when cell = Total. This is because flag = 1 when the cell = Ingresos

I often resort to using a quick Flowchart rather than drowning in recursive if…else :grinning:

@sudster

Thats true…but if you see the scenario above they are dependant…values for same variable are different in each condition

Flag will be 1 for the following iteration and not the current

Cheers

Perhaps I wrote the pseudocode incorrectly, but the real code does work. However, the purpose of the question was to find out if there was a way to simplify when multiple if statements appear in succession, as it is very annoying to correct or follow a workflow when that happens. It is not very readable.

This is the dark side of low code :frowning:
You could use coded workflow instead.
Cheers

You can just use an Assign activity and write out the if statement.

var = If(x = y, true, false)

Yes, that is possible. But I don’t see the flag variable initialised outside the for loop. Anyway, there are multiple ways to skin a cat!

@sudster

Agreed…

But in this case…say in first iteration value is ingress the flag changes to 1…

Then it can be a total and 1 combination as well…

Cheers

Hello, thank you all for your responses. However, the purpose of the question was to find a way to simplify the code “visually.” The code works, but the chaining of IF statements in UiPath seems unfriendly to me because it does not allow the removal of “Else” clauses. Perhaps, as @J0ska mentioned, that is the way to go. But it will have to wait for another time, when I have regained energy to tackle that topic.

Have you seen Microsoft PowerAutomate? This is unfriendly :smiley: