Preliminary knowledge
Through the learning of the preliminary tutorial, I believe you have mastered the most basic concepts and operations of Laiye Automation Platform, and can write the simplest process. This chapter will begin with the tutorial of Laiye Automation Platform intermediate edition. You will learn powerful and practical functions such as UB programming language, data processing, network and system, artificial intelligence, command Extension, etc.
In the tutorial of the Preliminary Edition, we hardly need to touch the Laiye Automation Platform programming language, and only touch a few simple data types. In the intermediate version of the tutorial, we will be exposed to some composite data types, such as arrays and dictionaries. This chapter first gives a brief introduction to the concepts of these two data types, because these two composite data types are often used in subsequent content.
Array
Still use the example in the tutorial of the preliminary version, as shown in the following figure. This is an Excel table. Each row in the table is an order record, and each column is different fields of the order, including order number, customer name, order quantity and sales volume.
As mentioned above, different types of variables can be used to save the data in this Excel table, for example, string variables can be used to save customer names, and integer variables can be used to save order quantities.
How to save multiple data at the same time? For example, order numbers of 100 order records need to be saved: one method is to define multiple variables, for example, 100 variables such as No1, NO2, NO3,..., no100 are used to save 100 order numbers, and one variable is used for each order number. This method is relatively simple and straightforward, but it will be very cumbersome when the data volume is large; Another smarter way is to use a array Composite type of. The so-called "array" refers to a group of elements that can be used to store multiple data, and these elements can be represented by a variable. The specific use method is: use commas to separate each element and square brackets to enclose it. Such a whole, that is, it forms an "array", which can be placed in one variable (without multiple variables). As shown below:
ArrayVariable = [No1, No2, No3, No4]
The values of multiple elements in the same array can be of any type. For example, if the value of an element is an integer, it constitutes an integer array. The data types of multiple elements in the same array can be the same or different, for example, the first element is an integer, and the second element is a string. Even the elements in an array can be another array, thus forming a multi-dimensional array in a general sense.
Generally, we only use two-dimensional arrays, and three-dimensional or more dimensional arrays are rarely used. The following is a typical two-dimensional array. The array contains two elements, and each element is an array, which contains six strings:
TwoDimensionArrayVar = [[ "Jordan", "Michael", "Tony", "Candy", "Brendan", "Jack" ],
[ "20K", "18K", "15K", "12K", "10K", "10K" ]]
So how to locate and access multiple elements in the array? This requires subscript The so-called subscript refers to the numerical number used to distinguish each element of the array. Generally speaking, the subscript of the array is the number of elements in the index group. However, the subscript of the array is numbered from 0. For example, the first element of the array variable is as follows:
ArrayVariable[0]
In this example, ArrayVariable[0]
Refers to No1
The value of this variable. If you want to reference the value of a two-dimensional or multi-dimensional array, use multiple subscripts:
TwoDimensionArrayVar[0][1]
The result is the" Michael " This value.
Dictionaries
In addition to arrays, there is another method called Dictionaries You can also implement a variable to save multiple data. However, the typical application scenario of an array is mainly used to store multiple data of the same nature and category, such as 100 order numbers; The application scenario of the dictionary is broader. It is mainly used to save multiple related data with different data types, such as the four fields of an order. In order to better access these fields of different data types, the dictionary stores not only the value of the data but also the name of the data.
The representation of dictionary type variables is: separate multiple elements with commas, and then enclose them with braces. Where each element must Contains one name And one value , Names and values are separated by colons. As shown below:
{ Name1:Value1, Name2:Value2, Name3:Value3 }
Among them: name It can only be a string, value Can be any type of expression. If you are familiar with JavaScript or JSON, you will find that this initialization method is highly similar to the JSON representation.
The above order records can be expressed as follows:
DictVar ={ "OrderNum":"3", "CustomerName":"Jordan", "OrderQuantity":6, "Sales":261.54 }
Similarly, a dictionary can also use the subscript as an index to access its elements, but the dictionary index is name , This is a string. For example, the method to obtain the order number of the dictionary variable is:
DictVar["OrderNum"]
The result is the value of the element named "Order No." in the dictionary, that is, the string "3"
.