Today, every software application, business process, or human deals with data. Data is the most critical resource in every organization and it's available to us in many different forms. We need to modify, structure, format, or sort data to convert it into meaningful information in a business process.
This chapter focuses on introducing different methods of manipulating data based on various data types. Data used in a business process undergoes many transformational steps before it's ready for consumption at various stages of the process, hence it is crucial to understand how to perform such manipulations.
This chapter consists of the following sections:
As a prerequisite, please have UiPath Studio and UiPath Robot installed and connected with Orchestrator. Furthermore, it is essential to understand the different data types, control flows, and UiPath Studio features before starting this chapter.
Data manipulation is a process that alters data through various operations to convert it into a usable format. It is a vital function for business operations and management as it provides many benefits, such as the following:
Data manipulation operations consist of four primary functions, as outlined here:
There are many different methods of manipulating data, depending on the type of data. Let's now take a look at various data manipulation techniques.
Strings are one of the most commonly used data types in any automation solution. A string data type refers to a series of text values that we frequently use to extract, display, process, and send data between multiple applications for further processing. String operations help perform business scenarios such as sending out emails for users, extracting a portion of text from a larger text, and so on.
UiPath uses string manipulation methods in Visual Basic .NET (VB.NET) or C#, depending on the language you choose to use in UiPath Studio. In this chapter, we will use the methods in VB.NET. The following sections describe some of the commonly used string manipulation methods in UiPath.
The Concat function enables the user to concatenate two or more string representations into one string representation. The Concat function is capable of concatenating up to four string objects at a time. The following code snippet shows the expression we use to concatenate strings:
String.Concat(StrVariable1, StrVariable2)
Let's take a look at a simple example of how to use this function. Proceed as follows:
String.Concat(DisplayText, FirstName, LastName)
Once you reach this point, your workflow should look similar to this:
Now we know how to combine two strings, let's look at how we can check whether a particular string exists in another string.
The Contains function checks whether a specified substring exists in another string. The function returns a Boolean value depending on the result. The function returns True as the output when the substring exists in the main string and returns False if not. An important point to remember is that this function is case-sensitive; hence, it may return False as the output even when two strings match, but the casing of the letters is different. As we discussed in Chapter 5, Learning about Variables and Arguments, Boolean results enable the user to control the workflow execution. Since the Contains function returns a Boolean result, this function is mainly associated with a control flow activity. We can either use an If activity or Flow Decision activity depending on whether we are using a Sequence or a Flowchart container.
Let's look at the same workflow we designed and change it by adding this function to test its functionality, as follows:
String.Concat(DisplayText, FirstName, LastName).Contains("John")
The expression concatenates all the string variables and checks whether the "John" substring is available in it.
"The name you are looking for is not available"
Once you configure the Write Line activity, your workflow should look similar to this:
The String.Contains function gives you True as the result if the searched string is available. If you want the function to return False when the string exists (simply by reverting the result), you can introduce the Not keyword into the expression. In the previously described example, the expression would look like this after introducing the Not keyword:
Not String.Concat(DisplayText, FirstName, LastName).Contains("John")
We can also look at another function that is similar to String.Concat. The String.Format function, however, provides the ability to change values at designated locations in a defined string. The next section explains this concept in detail.
The Format function is similar to the Concat function. However, the Format function can convert objects into strings and insert them into another string at specified locations. The Format function also allows the combining of any number of strings into one string variable. The Format function uses specific identifiers (IDs) to locate where to insert new text in the primary string. Such IDs are referred to as {0}, {1}, and {2}, in which the numeric values describe the order of merging text.
Let's take a look at how we can apply the Format function through a simple example, as follows:
String.Concat(DisplayText, FirstName, LastName)
"Good day… Let me introduce myself. {0} and I am {1} years old. I'm currently based in {2}"
String.Format(FormatString, ConcatenatedText, "29", "Sri Lanka")
If you look at the function closely, the FormatString variable is the primary string that we need completed. The {0}, {1}, and {2} indicators are replaced by the values specified in the expression in the specified order.
Once you complete the configuration, your workflow should look similar to this:
The String.Format option can be helpful in scenarios where you have to replace dynamic parts of a string with values retrieved during execution. One good example is updating the body of a standard email message with the name of the person addressed. So, let's look at the Replace function.
The Replace function enables the user to replace all occurrences of a specified substring in the primary string. Similar to the Contains function, this function is also case-sensitive. It uses the following expression to replace text:
VarName.Replace(<String to Replace>, <New Value>)
The Replace expression requires two parameters: the string to be replaced and the new text.
Let's apply this function to the same workflow and try replacing some values, as follows:
DisplayResult.Replace("Hello…", "")
This code would look for the word "Hello…" in the string value in the DisplayResult variable and replace it with a blank string.
So, the Replace function is beneficial when you want to remove special characters or any unwanted text from a string.
At this point, you are now familiar with several methods under string manipulation. Along with these methods, we sometimes need to search for a location of a string and get the position to perform certain actions. The following section describes the use of the IndexOf method to perform search operations.
The IndexOf function returns the index of the first occurrence of a specified character in a string. An index in programming always starts at 0. Hence, in an actual string, the first letter is the zeroth letter, the second letter is the first letter, and so on. It is not just another method; it is one of many overload methods in VB.NET that perform several search mechanisms based on different input parameters we provide. In general, it uses the following expression to obtain the index of a specified character:
StrVariable.IndexOf("<search string>")
Let's get the index of the first occurrence of a letter in our previous output string, as follows:
CleansedValue.IndexOf("d").ToString
This configuration would provide the index of the first occurrence of the letter "d" in the CleansedValue string. The index is next converted into a string using the .ToString method to print it as text in the Message Box activity.
Apart from what we discussed, there are several overload methods that return the index of a string. We will not get into the detail of each and every method; however, we need to know the basics. Some overload methods are listed here:
As we tried one type out of the different overload methods, you can also try out some of the other types mentioned to get more practice on obtaining the index of a given string.
Let's now apply the knowledge gained on indexes and try out another method for string manipulation.
There are scenarios where you need to extract only a particular portion of a string from a longer string to process it further. For example, from all the text in a Portable Document Format (PDF) file, you might want to extract only the invoice number to use it in another application. The SubString function comes in handy in such scenarios as it allows extraction of text by providing the starting index and the length of the substring. It uses the following expression to extract a portion of a text from a longer string:
StrVariable.SubString(<starting index>, <length of substring>)
Applying our knowledge of indexes, let's use the same working solution and try to extract the name of the person from the string variable, as follows:
CleansedValue.Substring(47, 8)
We can try to get the same output using the IndexOf function without hardcoding the positions. As we explored several IndexOf functions, let's explore a bit how we can combine these two functions together to get the same output.
As a quick recap, this section focused on extracting the desired part of a string from a larger string by providing start and end index points. This function comes in very handy on many occasions where you want to extract certain parts of a string based on a specific string pattern. Apart from this function, we also have the ability to split a string into multiple elements and access each part separately. Let's now see how we can split a text and retrieve individual elements after splitting.
The Split function enables the user to split a long string based on a specific character. This functionality can be used when you need to work with comma-separated values (CSV) files or delimited string variables to split to access each component. Splitting a single string based on a delimiter converts the entire string into a string array representing each element by a zero-based index. It uses the following function to split string values. The arrays are of fixed size; hence, you cannot add or remove items from an array after creation:
StrVariable.Split("<split character>"c)
The letter c after the split character converts the character specified within double quotes to a character type.
Let's use our previous examples and split the values in a CleansedValue variable, as follows:
CleansedValue.Split(" "c)
The aforementioned configuration will split the entire sentence in the CleansedValue variable by a space delimiter. Each word will represent a specific element in the array based on its position in the string. For example, splitting the "Hello World" string with a space places the two words in the zeroth and first indexes in the array.
SplitArray(0).ToString
Once you reach this point, the workflow you built should look similar to this:
This section focused on learning how to split a string by providing the split character. This function comes in handy when you want to extract specific text elements from a given string. Further, we also learned that the elements in the split array can be accessed through a zero-based index. Having said that, let's now look at how we can merge data in collection variables—such as arrays—into one string.
The Join function combines all the elements in a string array into one string by introducing a character as a separator between each element. For example, if the array consists of two elements such as "Hello" and "World", the separator introduced in the Join function is included in the output string to indicate which position it was split originally. Hence, in our example here, if we introduce "|" as the separator, the Join function returns "Hello|World" as the output. The expression used for the Join function is shown here:
String.Join("<separator>", <Collection Variable>)
Let's apply this function to our previous working solution to join the elements in the SplitArray variable, as follows:
String.Join(" ", SplitArray)
We specified the space as a separator to return the string to its original form.
String manipulation methods help us to understand which standard functions we can perform using text values. The concept of indexes we learned in this section of the chapter is common across all connection-type variables such as arrays, lists, data tables, and so on. All collection variables in VB.NET use the same concept using zero-based indexes when referring to individual elements. Each data type has its specific functions.
Similar to String functions, date-related functions are unique to the DateTime data type. Date-related calculations are quite common across any automation, hence it is essential to understand functions related to date values.
Date and time are two types of values that we come across almost all the time. Similarly, organizations deal with data that is of different dates, months, and years. .NET Framework offers many functionalities related to date values, enabling the user to calculate and manipulate dates easily. However, it is not required to know all the functionalities for the Associate exam. Hence, let's only look at some of the functionalities, which include the following:
Let's explore some of the functionalities with the following scenario. We need to obtain a list of dates between 7 days before the current date and 7 days after the current date. Further, we also want to print the date, month, and the name of the date. The following steps provide a guide on generating the required output:
The specified code deducts 7 days from the date held in the CurrentDate variable to generate a new date.
CurrentDate.AddDays(7)
The specified code adds 7 days to the date specified in the CurrentDate variable to generate the maximum date we need to extract.
ProcessDate<= MaximumDate
"Date: " + ProcessDate.ToString("dd-MMMM-yyyy") + " | Month: " + Microsoft.VisualBasic.DateAndTime.MonthName(ProcessDate.Month) + " | Day: " + ProcessDate.ToString("dddd")
The ProcessDate.ToString("dd-MMMM-yyyy") code segment converts the date to the specified format to output the date in string format. For example, the date is printed as 01-January-2021.
The Microsoft.VisualBasic.DateAndTime.MonthName(ProcessDate.Month) code segment generates the month name of the date value stored in the variable. For example, if the provided date is 01/12/2020, this code's output would be "December".
The ProcessDate.ToString("dddd") code segment converts the current day into the day's name, such as Sunday, Monday, and so on.
ProcessDate.AddDays(1)
Once you reach this point, you have completed creating a date manipulation workflow. The developed workflow should look similar to this:
There are many functions related to a date. However, this section of the chapter covers only a few of the most commonly used functions. Based on the general idea obtained by going through the previously mentioned steps, you can easily use other functions to manipulate the data to retrieve the required output. On the other hand, the Lists data type offers the capability to hold a list of values of a specific data type, including strings, date-times, integers, and so on. The next section of the chapter focuses on some of the commonly used functions when working with list variables.
Lists are data types of a specific type that can contain multiple data values. All list data values have a fixed memory location, which is known as an index. Arrays are of fixed size and defined at the creation of a variable. However, lists allow us to add, modify, and remove items during runtime. Similar to arrays, lists provide specific methods to manipulate their data, such as the following:
UiPath offers a collection of activities to perform the aforementioned actions efficiently. The activities that UiPath offers are listed here:
Let's now practice some simple list manipulation methods. The steps outlined next provide a guideline to perform the required actions.
In this section of the chapter, we will take a look at how to declare list variables through the Variables panel.
Open UiPath Studio and create a new Process activity, as follows:
New List(Of String) from {"John", "Daniel", "Keith"}
New List(Of String) from {"Diana", "Lizi", "Peter", "Daniel"}
You are now familiar with the creation of list variables. Let's now focus on using variables in workflows.
In this section, we will look at the usage of the Add to Collection activity to add items to one of the lists we created earlier. Proceed as follows:
"Andrew"
This error is thrown because the collection expects a string data type, but the activity is automatically configured to add object data types. You can see this configuration in the TypeArgument property of the activity.
As we have built the workflow to add items to list variables, the next section of the chapter focuses on merging the two lists.
Merging lists cannot be done directly—it requires converting the List type into an Enumerable type. To do this, proceed as follows:
Enumerable.Concat(StudentsList.AsEnumerable, EmployeeList.AsEnumerable).ToList
By now, we have one List variable that we can use for further processing. Further, if we look closely at the data values we have in both lists, we can see that the name "Daniel" is available in both lists. Once we merge the two lists into CombinedPeopleList, the new list now contains duplicate entries for "Daniel". The next section of the chapter focuses on how to remove duplicate entries.
Data removal from a list is made easy by the Remove From Collection activity. We can use this activity to remove the duplicate entry we have for "Daniel" by following these next guidelines:
The data is now ready for modification. The next section of the chapter focuses on how to convert data in a list to uppercase.
Lists contain multiple data values that we can access using an index. Similarly, this functionality also enables the user to loop through data elements while performing different actions for each data element.
Let's take a look at how to loop through items and update each item in a list. In our scenario, we will update items in the list by converting them to uppercase letters. Proceed as follows:
StrConv(item, VbStrConv.Uppercase)
String.Join("|", ModifiedList)
Once you reach this point, the workflow you built should look similar to this:
Similarly, the final configuration of the variable you created in the Variables panel should look similar to this:
JOHN|KEITH|ANDREW|DIANA|LIZI|PETER|DANIEL|JANE
The steps performed in this section give you a good understanding of how to manipulate data using List variables. Compared to arrays, the additional features List variables offer—such as adding or removing elements during runtime—make life easy when processing data elements. Lists are capable of holding any type of value, including numeric values. Numeric data has its unique functionalities such as summing up, division, multiplication, and so on. The next section of the chapter focuses on a few commonly used numeric calculations.
Numeric data types come in handy while performing different simple-to-complex calculations. We can perform many calculations using numeric values, such as summing up, calculating averages, finding minimum and maximum values, and so on. Performing numeric calculations in UiPath is pretty easy. Let's try a straightforward example.
Create a List variable that holds the marks a student obtained during the last semester for all five subjects. Our task is to calculate the total and the average of the marks. Proceed as follows:
New List(Of String) From {"75", "34.4", "54", "34", "55"}
The scores on the list are of the String type. We cannot use the String data type to perform the calculations. As a result, we need to convert them to numeric data types while we perform the calculation.
TotalScore + Convert.ToInt32(Math.Round(Double.Parse(item)))
TotalScore / StudentMarks.Count
There are many functions available in .NET Framework to perform numeric calculations. The example provided here is fundamental. It is intended to give you a high-level idea of performing some numeric manipulations such as rounding up values, calculating totals, and deriving averages. The knowledge you already gained through this chapter will give you the foundation to explore other functions in the Math library and the different other functions available for numeric calculations. Explore other methods and familiarize yourself with those functions as they will be useful when building automation solutions for your organization.
While numeric data types have their unique capabilities, Dictionary-type variables offer a unique way of managing data using the key-value pair concept. The next section of the chapter explains how to manipulate data using Dictionary variables.
The Dictionary type has a key difference compared with List and Array types. While lists and arrays hold multiple data elements of the same data type, a dictionary can hold each data element along with a unique key. This combination is named key-value pairs. The user chooses the data type of the key and the value during variable creation.
The Dictionary type is mainly used for storing configuration details or other similar information. You may wonder why you should not use a List type to store and access configuration data. In real-world scenarios, each configuration data element has a descriptive name that describes what the configuration represents. For example, if the configuration file has data on Uniform Resource Locators (URLs), timeouts, and file paths, the file may look like this:
Dictionary variables in similar scenarios make it easier for the user to understand which values are used within the workflow because of the descriptive key. This same concept is impossible with List or Array variables because values are accessed only by an index in lists and arrays.
UiPath Studio offers an environment to work with dictionaries with ease to perform actions such as initiation, addition, removal, extraction, and reassigning values. Let's work on a simple example to understand how to work with dictionaries.
A dictionary is also a collection variable like lists and arrays. Therefore, the initialization procedure is similar to those data types. Initialization occurs when creating a variable through the Variables panel or within a workflow using an Assign activity.
Let's create a simple Dictionary variable that holds personal information such as a person's name, age, country, and birth date. Proceed as follows:
New Dictionary(Of String, String)
The code provided initializes the dictionary with a new Dictionary object of the specified type to make it ready for later use. Now, let's assign some values to the new Dictionary variable.
Since we now have a usable variable, let's add new values to the dictionary. This section will add new items such as the name, age, country, and date of birth of a person to the dictionary. Proceed as follows:
The Parameters screen should look similar to this once you complete the aforementioned steps:
We now have data in the variable. Let's try to extract the data we inserted to use it in our workflow.
You are familiar with extracting data from lists and arrays. We used an index to access data elements in Array and List variables. Similarly, in Dictionary variables, we use the key to access data elements instead of the memory index. To do this, proceed as follows:
PersonalData("Name").ToString
Now that you have reached this point, you should be familiar with manipulating data in Dictionary variables. The steps provided in this section enable you to easily initialize, assign, and retrieve values from Dictionary variables to use in real-world use cases. The use of Dictionary variables is similar to List and Array variables. The only difference is that Dictionary variables' values are accessed through a unique key instead of the memory location.
We discussed extracting, converting, and performing several other manipulations on different data types during this chapter. Up to now, we discussed pretty straightforward situations where you had the exact value you need to manipulate. However, there are scenarios where you need to extract specific values based on a pattern from larger datasets, such as text paragraphs. We need to use unique functionalities such as regexes to extract only the value we need in such scenarios.
The importance of regexes is that it enables the user to match, locate, and extract values based on a specific pattern. For example, if you have a text paragraph that contains account numbers and email addresses, you can use regexes to extract only email addresses from the input string.
However, building regexes can be challenging. UiPath provides a regex builder to simplify the complexities around these expressions. Regexes are commonly used to parse text and for input validation, data scraping, and string manipulation. Hence, regexes are a part of string manipulation. However, we thought of addressing regexes separately as they are quite different from the other manipulation methods that we have covered.
UiPath provides three methods that use regexes along with RegEx Builder. Each method is a separate activity in UiPath that is associated with RegEx Builder. The three methods are listed here:
Each activity includes a button that enables you to navigate to the RegEx Builder screen, allowing you to build the activity's regex. The RegEx Builder screen looks like this:
The sections of the RegEx Builder screen are explained as follows:
Let's try some of these options with a simple example. We are given the following string:
"You have an amazing opportunity to connect with one of the celebrities to ask questions. Please reach out to [email protected] or [email protected] to reserve your seat today. For more information, reach out to 5992754159."
We need to use and extract all the email addresses available in the string. To do this, proceed as follows:
The data we receive during any process may not always be perfect. As a result, extracting the expected data and transforming it to the format we need is always mandatory. We always work with data; therefore, knowing how to manipulate data using different methods comes in handy when building automation solutions. There are straightforward transformation methods, such as the methods we have for string, integer, and date-time formats. However, regexes come in handy when we work with unstructured data where extraction is not straightforward.
Manipulating data while building automation workflows is very important. Data manipulation enables you to read, filter, sort, extract, and perform calculations to derive meaningful data out of raw input data. Different variable types have their unique ways of manipulating data. Therefore, it is essential to understand which functionalities UiPath offers to perform various manipulations based on each data type. This chapter provided you with a good understanding of how to manipulate data in the most commonly used variable types such as strings, arrays, lists, and dictionaries.
The concepts learned in this chapter are very helpful in building complex workflow solutions with many manipulations. The following chapters use the basic knowledge gained in this chapter to perform complex actions such as interacting with Excel files, emails, user interfaces (UIs), and so on. As a result, it is essential to understand the concepts and methods described in this chapter. The next chapter covers one of the most critical topics in UI automation: selectors. The concepts you learned throughout this chapter will prove useful while performing UI automation.
18.118.1.232